Binding "SelectedItems" of a Listbox

Mar 24 2019 7:00 AM

Hi,

Could you please help me figure out how I could bind "SelectedItems" of a listbox?

Here I have a 'listbox_2' that contains a list of fruits and vegetables ;)
There's another 'listbox_1' that contains groups of fruits and vegetables (under a list format...maybe is it my first error? Should it rather be a list of fruits/vegetables?) and a recipe.

I would like that on each group selection (in listbox_1), the corresponding fruits and vegetables would be selected (in listbox_2)...and make it possible to modify that list

Unfortunately, the way to achieve this behavior stays obscure to me...could you please help?

Here's what I've set up untill now:

C#

  1. public partial class MainWindow : Window  
  2.     {  
  3.         ItemList il;  
  4.         GroupList gl;  
  5.   
  6.         public MainWindow()  
  7.         {  
  8.             InitializeComponent();  
  9.         }  
  10.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  11.         {  
  12.             il = new ItemList();  
  13.             ICollectionView cvs = CollectionViewSource.GetDefaultView(il);  
  14.             cvs.SortDescriptions.Add(new SortDescription("_type", ListSortDirection.Ascending));  
  15.             cvs.SortDescriptions.Add(new SortDescription("_name", ListSortDirection.Ascending));  
  16.             cvs.GroupDescriptions.Add(new PropertyGroupDescription("_type"));  
  17.             ListBox2.ItemsSource = cvs;  
  18.   
  19.             gl = new GroupList();  
  20.             ICollectionView cvt = CollectionViewSource.GetDefaultView(gl);  
  21.             ListBox1.ItemsSource = cvt;  
  22.         }   
  23.     }  
  24.   
  25.     public class Item  
  26.     {  
  27.         public string _type { getset; }  
  28.         public string _name { getset; }  
  29.         public Item()  
  30.         {  
  31.         }  
  32.     }  
  33.     public class ItemList : ObservableCollection {  
  34.         public ItemList() {  
  35.             base.Add(new Item() { _type = "fruit", _name = "apple" });  
  36.             base.Add(new Item() { _type = "vegetable", _name = "potato" });  
  37.             base.Add(new Item() { _type = "fruit", _name = "banana" });  
  38.             base.Add(new Item() { _type = "vegetable", _name = "tomato" });  
  39.             base.Add(new Item() { _type = "fruit", _name = "pear" });  
  40.             base.Add(new Item() { _type = "vegetable", _name = "salad" });  
  41.             base.Add(new Item() { _type = "fruit", _name = "orange" });  
  42.             base.Add(new Item() { _type = "vegetable", _name = "onion" });   
  43.         }  
  44.     }  
  45.   
  46.     public class Group  
  47.     {  
  48.         public string _groupname { getset; }  
  49.         public List _members { getset; }  
  50.         public string _recipe { getset; }  
  51.         public Group()  
  52.         {  
  53.         }  
  54.     }  
  55.     public class GroupList : ObservableCollection  
  56.     {  
  57.         public GroupList()  
  58.         {  
  59.             base.Add(new Group() { _groupname = "Group_1", _members = new List() { "apple""salad" }, _recipe = "Do this and do that" });  
  60.             base.Add(new Group() { _groupname = "Group_2", _members = new List() { "banana""onion" }, _recipe = "Don't do that and do this" });  
  61.         }  
  62.     }  
XAML
  1. <Window x:Class="WpfApplication1.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         Title="MainWindow" Height="350" Width="525"    
  5.         Loaded="Window_Loaded">    
  6.     <Grid>    
  7.         <Label Margin="12,0,378,283" Content="Group"></Label>    
  8.         <Label Margin="190,0,200,283" Content="Members"></Label>    
  9.         <Label Margin="309,0,81,283" Content="Recipe"></Label>    
  10.         <TextBox Margin="309,34,12,12" DataContext="{Binding SelectedItem, ElementName=ListBox1}" Text="{Binding Path=_recipe, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>    
  11.         <ListBox Margin="12,34,378,12" Name="ListBox1" SelectionMode="Single">    
  12.             <ListBox.ItemTemplate>    
  13.                 <DataTemplate>    
  14.                     <TextBlock Text="{Binding _groupname}" />    
  15.                 </DataTemplate>    
  16.             </ListBox.ItemTemplate>    
  17.         </ListBox>    
  18.         <ListBox Margin="190,34,199,12" Name="ListBox2" SelectionMode="Multiple" SelectedValuePath="_name">    
  19.             <ListBox.ItemTemplate>    
  20.                 <DataTemplate>    
  21.                     <TextBlock Text="{Binding _name}" />    
  22.                 </DataTemplate>    
  23.             </ListBox.ItemTemplate>    
  24.             <ListBox.GroupStyle>    
  25.                 <GroupStyle>    
  26.                     <GroupStyle.ContainerStyle>    
  27.                         <Style TargetType="{x:Type GroupItem}">    
  28.                             <Setter Property="Template">    
  29.                                 <Setter.Value>    
  30.                                     <ControlTemplate>    
  31.                                         <Expander Header="{Binding Name}" IsExpanded="True">    
  32.                                             <ItemsPresenter />    
  33.                                         </Expander>    
  34.                                     </ControlTemplate>    
  35.                                 </Setter.Value>    
  36.                             </Setter>    
  37.                         </Style>    
  38.                     </GroupStyle.ContainerStyle>    
  39.                 </GroupStyle>    
  40.             </ListBox.GroupStyle>    
  41.         </ListBox>    
  42.     </Grid>    
  43. </Window>    
EDIT: from my reading, it seams that listboxes couldn't 'easily' bind 'SelectedItems'..maybe then a solution to bind to a checkbox component inside the listbox item?

Thanks for any kind help!