Lien entre 2 listbox wpf

Résolu
donkeydim Messages postés 6 Date d'inscription vendredi 15 janvier 2016 Statut Membre Dernière intervention 24 janvier 2016 - 15 janv. 2016 à 16:29
donkeydim Messages postés 6 Date d'inscription vendredi 15 janvier 2016 Statut Membre Dernière intervention 24 janvier 2016 - 18 janv. 2016 à 21:27
Salut,

Je cherche comment relier deux listbox wpf.

Par exemple quand je choisi ds listbox1 "fruits" ca m'affiche dans la listox2 "banane,cerise,clémentine... Et quand je choisi légumes dans listbox1 ca affiche ds listbox 2...

Je n'arrive pas a faire la data binding.

Merci.

1 réponse

Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
Modifié par Whismeril le 18/01/2016 à 17:58
Bonjour,

j'ai montré ta question à un collègue qui connait bien WPF.
Voici sa proposition:

Deux classes:
   public class aliment
    {
        public aliment(string Nom)
        {
            this.Nom = Nom;
            LP = new List<produit>();
        }

        public List<produit> LP { get; set; }

        public string Nom { get; set; }

        public override string ToString()
        {
            return Nom;
        }
    }
    public class produit
    {
        public string name { get; set; }
        public produit() { }
        public produit(string s) { name = s; }
        public override string ToString()
        {
            return name;
        }
    }


Le xaml (la couleur c'est juste pour que ça tranche)
        <Grid Name="Grid_Infos" Background="#FF4A4848">
            <ListBox x:Name="LBox2" ItemsSource="{Binding ElementName=LBox, Path=SelectedItem.LP}" Margin="258,0,0.4,-0.2" Background="#FFD2D2F2" ></ListBox>
        </Grid>
        <ListBox x:Name="LBox" Margin="0,0,293.4,-0.2"></ListBox>


Et le code behind dans Windows_Loaded
            produit p1 = new produit("orange");
            produit p2 = new produit("pomme");
            produit p3 = new produit("tomate");
            produit p4 = new produit("salade");
            aliment temp = new aliment("Fruits");
            aliment toto = new aliment("Légumes");
            temp.LP.Add(p1);
            temp.LP.Add(p2);
            toto.LP.Add(p3);
            toto.LP.Add(p4);
            LBox.Items.Add(temp);
            LBox.Items.Add(toto);


Quand j'étais petit, la mer Morte n'était que malade.
George Burns
1
donkeydim Messages postés 6 Date d'inscription vendredi 15 janvier 2016 Statut Membre Dernière intervention 24 janvier 2016
18 janv. 2016 à 21:27
ok merci ça répond bien à mon attente.

Je vais travailler avec cela sur mon projet.
0
Rejoignez-nous