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 Combobox en couleur


Sujet :

Windows Presentation Foundation

  1. #1
    Membre régulier

    Profil pro
    Inscrit en
    Février 2006
    Messages
    101
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 101
    Points : 121
    Points
    121
    Par défaut Custom Combobox en couleur
    Bonjour,

    Je cherche à faire un contrôle personnalisé à partir d'une Combobox. Suivant la valeur de l'item sélectionné, je cherche à lui donner une couleur de texte.
    ex : si l'item sélectionné = "Masculin" alors il passe en bleu

    Merci d'avance pour votre aide

    Laurent

  2. #2
    Membre habitué Avatar de Razorflak
    Homme Profil pro
    Développeur Flex/AS3
    Inscrit en
    Juin 2013
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Flex/AS3
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2013
    Messages : 97
    Points : 192
    Points
    192
    Par défaut
    Bonjour,
    Tu peux faire quelque chose comme ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
     
                if (comboBox1.SelectedItem.ToString() == "Masculin")
                    comboBox1.ForeColor = Color.Blue;
                else if (comboBox1.SelectedItem.ToString() == "Feminin")
                    comboBox1.ForeColor = Color.Pink;
     
                label1.Focus();
            }
    Je donne le focus à un autre élément pour que se soit plus visuel, sinon, le contenu de le CB reste en surligné.
    Dans ce cas, des que tu va réouvrir la CB toute les proposition seront de la couleur précedent choisi. Si tu veux que sa redevienne noire juste lors de la selection, tu peux modifier la couleur lors de l'évenement DropDown.

    En espérant t'avoir aidé.

    Cordialement.

  3. #3
    Membre régulier

    Profil pro
    Inscrit en
    Février 2006
    Messages
    101
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 101
    Points : 121
    Points
    121
    Par défaut
    Bonjour Razorflak,

    Merci pour ta réponse, je vais essayer. Mais je dois créer un custom control. Celui-ci va être intégré dans une datagrid qui est créée dynamiquement.

    Je sais c'est un peu compliqué mais je n'arrive pas à m'en sortir. J'arrive a le faire avec un user control mais pas avec un custom control

    Je te tiens au courant

    Laurent

  4. #4
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour l.mnu

    Suivant la valeur de l'item sélectionné
    1/la valeur dependra => de ton datatemplate...
    2/ l'item selectionne dependra =>du prop IsSelected du ComboBoxItem...

    Cela ne peut se faire que dans le datatemplate à cause de la valeur ...

    code xaml du winform :
    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
     
      <Window.Resources>
            <local:Persons x:Key="mySource"></local:Persons>
              <Window.Resources>
            <local:Persons x:Key="mySource"></local:Persons>
            <DataTemplate  
                x:Key="dt"
                DataType="{x:Type local:Personne}">
                <Border 
                    x:Name="border" 
                    BorderThickness="2" CornerRadius="6" >
                    <StackPanel 
                        Orientation="Horizontal">
                        <TextBlock 
                            Padding="10" 
                            Text="{Binding Nom}"/>
                        <TextBlock 
                            x:Name="tbGenre"
                            Padding="10" 
                            Text="{Binding Genre}">
                        </TextBlock>
                    </StackPanel>
                </Border>
                <DataTemplate.Triggers>
                    <MultiDataTrigger >
                        <MultiDataTrigger.Conditions>
                            <Condition Value="True" Binding="{Binding Path=IsSelected,
                                RelativeSource={RelativeSource 
                                AncestorType={x:Type ComboBoxItem}}}"/>
                            <Condition Value="Masculin" Binding="{Binding Path=Genre}"/>
                        </MultiDataTrigger.Conditions>
                        <MultiDataTrigger.Setters>
                            <Setter TargetName="tbGenre" Property="Foreground" Value="Red" />
                            <Setter TargetName="border" Property="Background" Value="Yellow" />
                            <Setter TargetName="border" Property="BorderBrush" Value="Pink" />
                        </MultiDataTrigger.Setters>
                    </MultiDataTrigger>
                    <MultiDataTrigger >
                        <MultiDataTrigger.Conditions>
                            <Condition Value="True" Binding="{Binding Path=IsSelected,
                                RelativeSource={RelativeSource 
                                AncestorType={x:Type ComboBoxItem}}}"/>
                            <Condition Value="Feminin" Binding="{Binding Path=Genre}"/>
                        </MultiDataTrigger.Conditions>
                        <MultiDataTrigger.Setters>
                            <Setter TargetName="tbGenre" Property="Foreground" Value="Blue" />
                            <Setter TargetName="border" Property="Background" Value="WhiteSmoke" />
                            <Setter TargetName="border" Property="BorderBrush" Value="Pink" />
                        </MultiDataTrigger.Setters>
                    </MultiDataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Window.Resources>
      <StackPanel >
            <ComboBox
                x:Name="cbo"
                Height="50"
                IsSynchronizedWithCurrentItem="True"
     
                ItemTemplate="{StaticResource dt}"
                ItemsSource="{StaticResource mySource}">
            </ComboBox>
        </StackPanel >
    </Window>
    code .cs du class personne exemple:
    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
     
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace WpfCombo
    {
     
        public class Personne : INotifyPropertyChanged
        {
            public Personne()
            {
                Nom = string.Empty;
                Prenom = string.Empty;
                Genre = string.Empty;
            }
            public Personne(string n, string pn,string gr)
                : this()
            {
                Nom = n;
                Prenom = pn;
                Genre = gr;
            }
            private string _nom;
            public string Nom
            {
                get { return _nom; }
                set
                {
                    _nom = value;
                    OnNotifyPropertyChanged("Nom");
     
                }
            }
            private string _prenom;
            public string Prenom
            {
                get { return _prenom; }
                set
                {
                    _prenom = value;
                    OnNotifyPropertyChanged("Prenom");
     
                }
            }
            private string _genre;
            public string Genre
            {
                get { return _genre; }
                set
                {
                    _genre = value;
                    OnNotifyPropertyChanged("Genre");
     
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnNotifyPropertyChanged(string propName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propName));
                }
            }
     
        }
        public class Persons : ObservableCollection<Personne>
        {
            private Personne p;
            public Persons()
            {
     
                for (int i = 1; i < 21; i++)
                {
                    string g = string.Empty;
     
                    if (i % 2 == 0)
                    {
                         g = "Masculin";
     
                    }
                    else
                    {
                        g = "Feminin";
                    }
                    p = new Personne("Nom"+i.ToString() ,"Prenom"+i.ToString(),
                       g);
                    this.Add(p);
                }
     
            }
        }
    }
    L'aspect visuel d'un controltemplate ne peut pas dependre des donnees.......
    bon code.....

  5. #5
    Membre régulier

    Profil pro
    Inscrit en
    Février 2006
    Messages
    101
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 101
    Points : 121
    Points
    121
    Par défaut
    Bonjour Mabrouki,

    J’ai essayé ta solution, elle fonctionne bien et j’ai appris maintenant comment fonctionnait un MultiDataTrigger. Maintenant cela ne correspond pas à mon problème. J’ai créé une datagrid dynamiquement dans lequel il y a des combobox. Je voudrai quand l’utilisateur choisit l'item "fait" dans une comboxbox, celle-ci se met avec un fond noir. J’ai trouvé une solution pour le faire en xaml et je cherche à le faire avec un custom control pour pouvoir l’implémenter dans ma datagrid qui est construit en code behind.
    Je te donne l’exemple en 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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    using System;
    using System.Windows;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
     
    namespace DataGridBinding
    {
        public partial class MainWindow : Window, INotifyPropertyChanged
     
        {
            public MainWindow()
            {
                InitializeComponent();
     
                var cb1 = new ObservableCollection<string>();
                cb1.Add("en valid");
                cb1.Add("toto");
                cb1.Add("fait");
                cb = cb1;
                DataContext = this;
                toto.SelectedItem = "fait";
            }
     
            private ObservableCollection<string> _cb = new ObservableCollection<string>();
            public ObservableCollection<string> cb
            {
                get { return _cb; }
                set { _cb = value; RaisePropertyChanged("cb"); }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
            public void RaisePropertyChanged(String _Prop)
            {
                if (PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(_Prop));
                }
            }
        }
    }
    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
     
    <Window x:Class="DataGridBinding.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:vw="clr-namespace:DataGridBinding"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <StackPanel Orientation="Vertical">
                <ComboBox Name="toto" Margin="20" ItemsSource="{Binding Path=cb}" >
                    <ComboBox.Style>
                        <Style TargetType="{x:Type ComboBox}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=toto}" Value="fait" >
                                    <Setter Property="Background" Value="black" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ComboBox.Style>
                </ComboBox>
            </StackPanel>
        </Grid>
    </Window>
    Merci pour ton aide, car je galère depuis quelques temps

    Laurent

Discussions similaires

  1. [Débutant] Question vb.net (textbox, combobox et couleur dégradé)
    Par kais.zekri dans le forum VB.NET
    Réponses: 9
    Dernier message: 08/01/2013, 22h59
  2. Combobox éditable : couleur du texte ajouté
    Par fouinette13 dans le forum Composants
    Réponses: 6
    Dernier message: 03/02/2012, 15h08
  3. ListBox ou ComboBox en couleur
    Par tonton67 dans le forum C++Builder
    Réponses: 5
    Dernier message: 29/03/2008, 16h13
  4. ComboBox et couleur de texte
    Par SoGood dans le forum VB.NET
    Réponses: 3
    Dernier message: 10/07/2007, 12h04
  5. Combobox avec couleur et font
    Par charaf dans le forum AWT/Swing
    Réponses: 16
    Dernier message: 27/06/2006, 20h12

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