能否请您帮我弄清楚如何绑定列表框的“ SelectedItems”?
在这里,我有一个“ listbox_2”,其中包含水果和蔬菜的列表;) 还有一个'listbox_1',其中包含水果和蔬菜的组(以列表格式...也许是我的第一个错误?应该是水果/蔬菜的列表吗?)和食谱。
我希望在每个组选择中(在listbox_1中),都将选择相应的水果和蔬菜(在listbox_2中)...并可以修改该列表
不幸的是,实现这种行为的方法对我来说还是很晦涩...您能帮忙吗?
这是我到目前为止设置的内容:
C#
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
<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>
编辑:从我的阅读中可以看出,列表框的“ SelectedItems”属性是只读的。也许这是绑定到列表框项目内的复选框组件的解决方案?
感谢您的任何帮助!