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 :

[WPF] combobox et checkbox


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut [WPF] combobox et checkbox
    bonjour a tous,

    j'ai a nouveau un probleme avec les composant wpf:

    j'ai une liste de string "langFiles" qui récupère les langues dispo pour mon appli.

    je veux afficher cette liste dans une combobox et je souhaite avoir chaque item sous la forme d'une checkbox.

    j'ai fait cela pour la mise en forme de ma combobox:

    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
    <ComboBox Name="cbboxLangue"
                          Width="70"
                          Height="30"
                          HorizontalAlignment="Right"
                          >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox>
                                <TextBlock Text=......./>
                            </CheckBox>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                    
                </ComboBox>
    sous C# j'ai lié ma combobox a ma liste:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    String[] langFiles = LanguageOptions.getLangFiles();
                cbboxLangue.ItemsSource = langFiles;
    dans ce cas si j'ai trois fichiers langues differents (nommés: english, french, german), je récupère bien ces trois string dans "langFiles", et ma combobox affiche bien trois checkbox.

    Par contre je n'arrive pas a afficher les noms de ces trois fichiers (english, french, german) dans la propriété "text" des checkbox.... je ne sait pas comment faire (les .... au niveau du textblock).

    quelqu'un peut m'expliquer? si possible avec un exemple concret car mes recherches sur le net on été infructueuses.

    Merci

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut
    c'est bon c'est resolu, je suis vraiment une bille.... me casse le tronc a faire 10000 essais avec des binding maching et binding truc, alors que la solution, c'est tout simplement:

    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
     
    <ComboBox Name="cbboxLangue"
                          Width="70"
                          Height="30"
                          HorizontalAlignment="Right"
                          >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox>
                                <TextBlock Text="{Binding}"/>
                            </CheckBox>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
     
                </ComboBox>
    bon maintemant reste plus cas coché le bon checkbox dont le textblock correspond a une variable "strLang"
    j'arrive a sélectionner le bon item avec ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    void MarkSelectedLang()
            {
                //Only on Load
                String strLang = LanguageOptions.getSelLang();
                for (int i = 0; i < cbboxLangue.Items.Count; i++)
                    if (cbboxLangue.Items[i].ToString() == strLang)
                        cbboxLangue.SelectedValue = strLang;
                  }
            }
    Mais je veux en plus checker cet item... d'ou l'interet de checkbox. Comment fait-on cela?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Août 2003
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 22

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut
    salut merci pour le lien mais je n'arrive pas a le mettre en pratique.

    De plus après réflexion je pense que des radiobuttons sont plus adaptés que les checkbox.

    j'ai donc testé ceci:

    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
     
    <ComboBox Name="cbboxLangue"
                          Width="100"
                          Height="40"
                          HorizontalAlignment="Right"
     
                          >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <RadioButton 
                                Content="{Binding}"
                                IsChecked="{Binding Path=IsSelected}"
                            />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
     
                </ComboBox>
    avec en C#:

    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
     
    public frmMain()
            {
                InitializeComponent();
                //updateLang();
                getLangItems();
                MarkSelectedLang();
            }
     
    private void getLangItems()
            {
                String[] langFiles = LanguageOptions.getLangFiles();
                cbboxLangue.ItemsSource = langFiles;
            }
     
            void MarkSelectedLang()
            {
                //Only on Load
               String strLang = LanguageOptions.getSelLang();
                for (int i = 0; i < cbboxLangue.Items.Count; i++)
                    if (cbboxLangue.Items[i].ToString() == strLang)
                        cbboxLangue.SelectedValue = strLang;  
                    }
    j'ai bien ma list de langue qui apparait dans ma combobox sous forme de radiobutton.

    Mais par contre dans la combobox, le radiobutton correspondant a la langue sélectionnée (strLang), n'est pas coché... cela vient de "void MarkSelectedLang()" mais je ne sais pas quoi mettre dedans pour avoir ce que je souhaite.

    de plus dans ma combobox je peux cocher plusieurs radiobutton, or je veux qu'un choix a la fois.

    comment faire cela?

  5. #5
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Regarde dans le toolkit appelé WPF Bag'O'Tricks, il y a un contrôle qui correspond à ce que tu souhaites faire (une ListBox avec des RadioButton)

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    362
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 362
    Par défaut
    re,

    merci pour le lien mais je suis confronté aux mêmes soucis.

    J'ai donc créé une nouvelle classe "RadioCombobox" à partir d'une classe trouvé sur internet.
    Cette classe en une combobox contenant des radiobutton en guise d'items.
    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
     
    <ComboBox x:Class="System.Windows.Controls.Custom.RadioComboBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib" >
     
        <ComboBox.Resources>
            <Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <RadioButton x:Name="radio" Click="ItemRadioClick" 
                                GroupName="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name}" >
                                <RadioButton.Content>
                                    <ContentPresenter 
                                        Content="{TemplateBinding ContentControl.Content}" 
                                        ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
                                        ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
                                        HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
                                        VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                                </RadioButton.Content>
                            </RadioButton>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.Resources>
    </ComboBox>
    C#:
    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
     
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
     
    namespace System.Windows.Controls.Custom
    {
        /// <summary>
        /// Interaction logic for RadioComboBox.xaml
        /// </summary>
        public partial class RadioComboBox : ComboBox
        {
            public RadioComboBox()
            {
                InitializeComponent();         
            }
     
            private void ItemRadioClick(object sender, RoutedEventArgs e)
            {
                ComboBoxItem sel = (e.Source as RadioButton).TemplatedParent as ComboBoxItem;
                int newIndex = this.ItemContainerGenerator.IndexFromContainer(sel); ;
                this.SelectedIndex = newIndex;
            }
     
            protected override void OnSelectionChanged(SelectionChangedEventArgs e)
            {
                base.OnSelectionChanged(e);
     
                CheckRadioButtons(e.RemovedItems, false);
                CheckRadioButtons(e.AddedItems, true);
            }
     
            private void CheckRadioButtons(System.Collections.IList radioButtons, bool isChecked)
            {
                foreach (object item in radioButtons)
                {
                    ComboBoxItem lbi = this.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
     
                    if (lbi != null)
                    {
                        RadioButton radio = lbi.Template.FindName("radio", lbi) as RadioButton;
                        if (radio != null)
                            radio.IsChecked = isChecked;
                    }
                }
            }
        }
    }
    Ma RadioComboBox "cbboxLangue" est lié à ma liste des langues dispo de cette façon:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     String[] langFiles = LanguageOptions.getLangFiles();
                cbboxLangue.ItemsSource = langFiles;
    Ca fonctionne correctement, ma combobox est bien rempli avec la liste "langFiles" et je peux cliquer sur les éléments.

    La dernière chose à faire et ou je bloque, c'est de pouvoir au lancement de ma forme, sélectionner et cocher dans ma RadioCombobox "cbboxLangue", la langue par défaut contenu dans le string "strLang".

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     String strLang = LanguageOptions.getSelLang();
    donc ma question est simple, comment sélectionner et cocher dans ma RadioComboBox l'éléments correspondant à "strLang"?

    j'arrive a sélectionner le bon éléments dans ma radiocombobox "cbboxLangue", mais pas à cocher le radiobutton, en utilisant le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    String strLang = LanguageOptions.getSelLang();
                for (int i = 0; i < cbboxLangue.Items.Count; i++)
                    if (cbboxLangue.Items[i].ToString() == strLang)
                        cbboxLangue.SelectedValue = strLang;
                    }
    comment cocher le radiobutton correspondant?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [WPF] Binding TwoWay CheckBox
    Par NeoKript dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 10/06/2010, 16h09
  2. comboBox avec checkBox
    Par gentelmand dans le forum Windows Forms
    Réponses: 2
    Dernier message: 10/03/2009, 18h55
  3. WPF ListView avec Checkbox, trouver l'item qui a été Checked
    Par fmadore dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 09/12/2008, 17h21
  4. [WPF] TreeView avec CheckBox en cascades?
    Par bakonu dans le forum Windows Presentation Foundation
    Réponses: 5
    Dernier message: 18/07/2008, 10h59
  5. [WPF] ComboBox, binding avec association LINQ
    Par tomlev dans le forum Windows Presentation Foundation
    Réponses: 11
    Dernier message: 30/04/2008, 00h30

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