C# “有约束力”;SelectedItems";列表框的

C# “有约束力”;SelectedItems";列表框的,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,您能帮我弄清楚如何绑定列表框的“SelectedItems”吗 这里我有一个“列表框2”,其中包含水果和蔬菜的列表;) 还有另一个“listbox_1”包含水果和蔬菜组(列表格式下…可能是我的第一个错误?应该是水果/蔬菜列表吗?) 我希望在每个组选择上(在列表框_1中),选择相应的水果和蔬菜(在列表框_2中)…并使其能够修改该列表 不幸的是,实现这种行为的方法对我来说还不清楚……你能帮我一下吗 以下是我到现在为止设置的内容: C# 公共部分类主窗口:窗口 { 项目清单il; 分组列表gl; 公

您能帮我弄清楚如何绑定列表框的“SelectedItems”吗

这里我有一个“列表框2”,其中包含水果和蔬菜的列表;) 还有另一个“listbox_1”包含水果和蔬菜组(列表格式下…可能是我的第一个错误?应该是水果/蔬菜列表吗?)

我希望在每个组选择上(在列表框_1中),选择相应的水果和蔬菜(在列表框_2中)…并使其能够修改该列表

不幸的是,实现这种行为的方法对我来说还不清楚……你能帮我一下吗

以下是我到现在为止设置的内容:

C#

公共部分类主窗口:窗口
{
项目清单il;
分组列表gl;
公共主窗口()
{
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
il=新项目列表();
ICollectionView cvs=CollectionViewSource.GetDefaultView(il);
Add(新的sortddescription(“_type”,ListSortDirection.升序));
Add(新的sortddescription(“_name”,ListSortDirection.升序));
添加(新的PropertyGroupDescription(“_类型”);
ListBox2.ItemsSource=cvs;
gl=新的组列表();
ICollectionView cvt=CollectionViewSource.GetDefaultView(gl);
ListBox1.ItemsSource=cvt;
} 
}
公共类项目
{
公共字符串_type{get;set;}
公共字符串_name{get;set;}
公共项目()
{
}
}
公共类ItemList:ObservableCollection{
公共项目列表(){
添加(新项(){{u type=“fruit”,\u name=“apple”});
base.Add(新项(){{u type=“vegeture”,\u name=“potato”});
添加(新项(){{u type=“fruit”,\u name=“banana”});
base.Add(newitem(){{u type=“vegeture”,\u name=“tomato”});
添加(新项(){{u type=“fruit”,\u name=“pear”});
base.Add(newitem(){{u type=“vegetar”,\u name=“sala”});
添加(新项(){{u type=“fruit”,\u name=“orange”});
添加(新项(){{u type=“vegeture”,\u name=“onion”});
}
}
公共课组
{
公共字符串_groupname{get;set;}
公共列表_成员{get;set;}
公共字符串_recipe{get;set;}
公共组()
{
}
}
公共类组列表:ObservableCollection
{
公共组列表()
{
添加(newgroup(){{u groupname=“Group_1”,{u members=newlist(){“apple”,“sala”},{u recipe=“dothis and dothis”});
添加(newgroup(){{u groupname=“Group_2”,{u members=newlist(){“banana”,“onion”},{u recipe=“不要这样做,做这个”});
}
}
XAML


编辑:据我所知,listbox“”SelectedItems”属性是只读的。那么,是否有一个解决方案可以绑定到listbox项内的复选框组件

谢谢你的帮助

也许这有帮助:也许这有帮助:
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" });
    }
}
<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>