1 pièce(s) jointe(s)
Bind les "SelectedItems" d'un Listbox
Bonjour, et d'avance merci du temps que vous voudrez bien me consacrer!
J'ai un listbox_2 qui contient une liste de fruits et légumes ;)
J'ai un autre listbox_1 qui peut contenir un groupe de fruits et légumes (sous forme de texte...c'est peut-être une première erreur? Peut-être faudrait-il que ce soit des objets fruits_légumes?) et une recette
Je voudrais qu'a chaque sélection d'un groupe (dans listbox_1) , l'on voit les fruits et légumes correspondants sélectionnés (dans listbox_2)
...et que cette sélection puisse être modifiée et mémorisée
Pièce jointe 460180
Malheureusement, tout cela reste très obscure pour moi...pourriez-vous m'aider à y voir plus clair!?
Voici le code jusqu'à présent:
C#
Code:
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
| public partial class MainWindow : Window
{
ItemList il;
GroupList gl;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
il = new ItemList();
ICollectionView cvs = CollectionViewSource.GetDefaultView(il);
cvs.SortDescriptions.Add(new SortDescription("_type", ListSortDirection.Ascending));
cvs.SortDescriptions.Add(new SortDescription("_name", ListSortDirection.Ascending));
cvs.GroupDescriptions.Add(new PropertyGroupDescription("_type"));
ListBox2.ItemsSource = cvs;
gl = new GroupList();
ICollectionView cvt = CollectionViewSource.GetDefaultView(gl);
ListBox1.ItemsSource = cvt;
}
}
public class Item
{
public string _type { get; set; }
public string _name { get; set; }
public Item()
{
}
}
public class ItemList : ObservableCollection<Item> {
public ItemList() {
base.Add(new Item() { _type = "fruit", _name = "apple" });
base.Add(new Item() { _type = "vegetable", _name = "potato" });
base.Add(new Item() { _type = "fruit", _name = "banana" });
base.Add(new Item() { _type = "vegetable", _name = "tomato" });
base.Add(new Item() { _type = "fruit", _name = "pear" });
base.Add(new Item() { _type = "vegetable", _name = "salad" });
base.Add(new Item() { _type = "fruit", _name = "orange" });
base.Add(new Item() { _type = "vegetable", _name = "onion" });
}
}
public class Group
{
public string _groupname { get; set; }
public List<String> _members { get; set; }
public string _recipe { get; set; }
public Group()
{
}
}
public class GroupList : ObservableCollection<Group>
{
public GroupList()
{
base.Add(new Group() { _groupname = "Group_1", _members = new List<String>() { "apple", "salad" }, _recipe = "Do this and do that" });
base.Add(new Group() { _groupname = "Group_2", _members = new List<String>() { "banana", "onion" }, _recipe = "Don't do that and do this" });
}
} |
XAML
Code:
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
| <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded">
<Grid>
<Label Margin="12,0,378,283" Content="Group"></Label>
<Label Margin="190,0,200,283" Content="Members"></Label>
<Label Margin="309,0,81,283" Content="Recipe"></Label>
<TextBox Margin="309,34,12,12" DataContext="{Binding SelectedItem, ElementName=ListBox1}" Text="{Binding Path=_recipe, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<ListBox Margin="12,34,378,12" Name="ListBox1" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding _groupname}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Margin="190,34,199,12" Name="ListBox2" SelectionMode="Multiple" SelectedValuePath="_name">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding _name}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}" IsExpanded="True">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
</Grid>
</Window> |