IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Custom Control avec different Styles


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Février 2011
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 24
    Par défaut Custom Control avec different Styles
    Bonjour,

    j'ai cree un custom control avec 2 styles,
    chacun avec son propre resource dictionnary et j'utilise un merge dictionnary dans themes/generic.xaml.

    en changeant l'ordre de declaration des merged dictionnary j'arrive a voir chacun de mes styles dans mon appli de test

    mais ce que je n'arrive pas a faire c'est de pouvoir selectionner le style que je veux. dans le property control, quand je click sur style, j'ai qu'un style de proposer.
    j'ai essayer d'avouter la proprietee x:key mais ca ne marche pas et le style qui l'as n'est plus utilisable.

    comment faire?

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour dominiqueFaure

    Bah,c'est comme ca que travaille WPF.Pour chaque control present wpf recherche son style dans l'ordre suvant :
    -niveau assembly d'Application(style perso avec x:key)
    -sinon assembly de librairie(cas des custom control) dans dossier Themes,fichier Generic.xaml
    -sinon dans PresentationFramework.dll...........
    Le style ayant ete trouve il "skippe" le reste.......

    Pour pouvoir faire ce que tu veux il faut
    1/ specifier comme suit ton dictionnaire de resources de style supplementaire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/WpfMyControlLib;component/Themes/BrushStyleRed.xaml" />
            <ResourceDictionary 
        </ResourceDictionary.MergedDictionaries>
    dans Generic.xaml avec nom qualifie de l'assembly hote (ta lib) dans Prop Source..
    2/Specifer le Style par defaut sans cle(x:key) explicitement dans Generic.XAML.....

    Dans l'appli d'utilisation referencer au niveau resource le dictionary Generic.xaml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    <Application x:Class="WpfTestThemes2.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml">
        <!--rajouter  au biveau  application(ou window ) cette ref..à generic.xaml...-->
        <!--pour voir les styles dans la fenetre Proprietes-->
        <Application.Resources>
            <ResourceDictionary  Source="/WpfMyControlLib;component/Themes/Generic.xaml"/>
        </Application.Resources>
    </Application>
    Exemple de code pour un CustomButton avec un style par defaut et 2 styles optionnels:
    1/projet lib
    code behind .cs du button:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace WpfMyControlLib
    {
        public class CustomButton : Button
        {
            static CustomButton()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
            }
        }
    }
    Fichier xam du dossier Themes:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
     
    <!--fichier generic.xaml-->
        <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfMyControlLib">
     
        <!--style par defaut pour le CustomButton-->
        <Style 
            TargetType="{x:Type local:CustomButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type local:CustomButton}">
                        <Grid Margin="5" Name="grid">
                            <Ellipse Stroke="DarkBlue" 
                                     StrokeThickness="2">
                                <Ellipse.Fill>
                                    <RadialGradientBrush
                                        Center="0.3,0.2"
                                        RadiusX="0.5" 
                                        RadiusY="0.5">
                                        <GradientStop 
                                            Color="Azure" Offset="0.1" />
                                        <GradientStop 
                                            Color="CornflowerBlue" Offset="1.1" />
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter 
                                Name="content" 
                                Margin="10"
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!--les MergedDictionaries-->
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/WpfMyControlLib;component/Themes/BrushStyleRed.xaml" />
            <ResourceDictionary Source="/WpfMyControlLib;component/Themes/BrushStyleCornsilk.xaml" />
        </ResourceDictionary.MergedDictionaries>
     
    </ResourceDictionary>
     
    <!--Fichier BrushStyleCornsilk.xaml-->
        <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfMyControlLib">
       <!--simple x:jey pour le style N0 1-->
        <Style 
            x:Key="StyleBrushCornsilk"
            TargetType="{x:Type local:CustomButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type local:CustomButton}">
                        <Grid
                            Margin="5" 
                            Name="grid">
                            <Ellipse 
                               x:Name="MyEllipse" 
                                Stroke="Brown" 
                                StrokeThickness="4"
                                Fill="Cornsilk">
                            </Ellipse>
                            <ContentPresenter 
                                Name="content" Margin="10"
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
     
            </Setter>
            <Style.Triggers>
                <Trigger  
                    Property="IsMouseOver" Value="true">
                    <Setter 
                       Property="Ellipse.Opacity" Value="0.4"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
     
    <!--Fichier BrushStyleRed.xaml-->
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfMyControlLib">
        <!--simple x:jey pour le style N0 2-->
        <Style 
            x:Key="StyleBrushRed"
            TargetType="{x:Type local:CustomButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type local:CustomButton}">
                        <Grid
                            Margin="5" 
                            Name="grid">
                            <Ellipse 
                               x:Name="MyEllipse" 
                                Stroke="CornflowerBlue" 
                                StrokeThickness="4"
                                Fill="Red">
                            </Ellipse>
                            <ContentPresenter 
                                Name="content" Margin="10"
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
     
            </Setter>
            <Style.Triggers>
                <Trigger  
                    Property="IsMouseOver" Value="true">
                    <Setter 
                       Property="Ellipse.Opacity" Value="0.4"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
    fichiersxaml utilisateur (app.xaml & winform.xaml):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
     
    <!--Fichier App.xaml-->
        <Application x:Class="WpfTestThemes2.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml">
        <!--rajouter  au biveau  application(ou window ) cette ref..à generic.xaml...-->
        <!--pour voir les styles dans la fenetre Proprietes-->
        <Application.Resources>
            <ResourceDictionary  Source="/WpfMyControlLib;component/Themes/Generic.xaml"/>
        </Application.Resources>
    </Application>
     
     
    <!--Fichier Window.xaml-->
    <Window x:Class="WpfTestThemes2.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mylib="clr-namespace:WpfMyControlLib;assembly=WpfMyControlLib"
            Title="Window1" Height="300" Width="300"     >
        <StackPanel>
            <mylib:CustomButton Content="ButtonDefaultStyle"></mylib:CustomButton>
            <mylib:CustomButton
                Content="ButtonCornsilk"
                Style="{StaticResource StyleBrushCornsilk}"></mylib:CustomButton>
            <mylib:CustomButton 
                 Content="ButtonRed"
                Style="{StaticResource StyleBrushRed}"></mylib:CustomButton>
        </StackPanel>
    </Window>
    bon code...............

Discussions similaires

  1. Custom Control avec numbre fixe d'element
    Par dominiqueFaure dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 06/03/2013, 10h08
  2. custom control avec des images
    Par Flamby38 dans le forum VB.NET
    Réponses: 4
    Dernier message: 20/12/2010, 02h36
  3. WPF: Custom control avec configuration étendue en design mode (as ActiveX)
    Par chiqlachiq dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 15/07/2008, 13h10
  4. Réponses: 9
    Dernier message: 21/06/2007, 16h02
  5. A propos du control 'tab' avec le style xp
    Par elf dans le forum Windows
    Réponses: 2
    Dernier message: 07/08/2006, 23h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo