首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ObservableCollection中绑定ObservableCollection

在ObservableCollection中绑定ObservableCollection
EN

Stack Overflow用户
提问于 2015-08-14 20:38:41
回答 3查看 425关注 0票数 2

我刚刚学会了如何将ComboBox绑定到ObservableCollection。喔呼!是否有方法将第二个ComboBox绑定到第一个ComboBox的选定集合?因此,每个项目都有一个ObservableCollection的Peices。当您选择一个项目时,我希望第二个ComboBox显示所选项目的代码!

代码语言:javascript
复制
public class Section
{
    public ObservableCollection<Item> Items { get; set; }

    public Section()
    {
        Items = new ObservableCollection<Item>();
    }

    public void AddItem()
    {
        string id = Items.Count.ToString();
        Items.Add(new Item("Item " + id));
    }
}

public class Item
{
    private string _name;

    public ObservableCollection<Peice> Peices { get; set; }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Item(string name)
    {
        _name = name;
        Peices = new ObservableCollection<Peice>();
    }

    public void AddPeice()
    {
        string id = Peices.Count.ToString();
        Peices.Add(new Peices("Peice " + id));
    }
}

public class Peice
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Peices(string name)
    {
        _name = name;
    }
}

<Grid>
    <ComboBox x:Name="cbItems" ItemsSource="{Binding Items}" DisplayMemberPath="Name" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Add" HorizontalAlignment="Left" Margin="55,33,0,0" VerticalAlignment="Top" Width="75" Click="AddItem"/>

    <ComboBox x:Name="cbPeices" ItemsSource="{Binding Item.Peices}" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button1" Content="Add" HorizontalAlignment="Left" Margin="55,94,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

更新: Ok,所以条目是'Item‘的列表。项目有一个'Peice‘的列表。我希望combobox 2显示所选项目的Peices集合的内容。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-14 20:56:50

绑定到第一个组合框的选定项,如下所示:

代码语言:javascript
复制
<ComboBox x:Name="comboBox1" ItemsSource="{Binding Items}" Width="150" Height="30" DisplayMemberPath="Name"></ComboBox>
<ComboBox ItemsSource="{Binding SelectedItem.Peices, ElementName=comboBox1}" Width="150" Height="30" DisplayMemberPath="Name"></ComboBox>
票数 2
EN

Stack Overflow用户

发布于 2015-08-14 20:43:06

只需将两者绑定到相同的事物,但添加双向绑定模式。所以:

代码语言:javascript
复制
<Grid>
    <ComboBox x:Name="cbItems" ItemsSource="{Binding Items, Mode=TwoWay, 
                UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" 
                HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" 
                VerticalAlignment="Top" Width="120"/>
    <Button x:Name="button" Content="Add" HorizontalAlignment="Left" Margin="55,33,0,0" 
                VerticalAlignment="Top" Width="75" Click="AddItem"/>

    <ComboBox x:Name="cbPeices" ItemsSource="{Binding Items, Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" 
                HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" 
                Width="120"/>
    <Button x:Name="button1" Content="Add" HorizontalAlignment="Left" Margin="55,94,0,0" 
                VerticalAlignment="Top" Width="75"/>
</Grid>
票数 0
EN

Stack Overflow用户

发布于 2015-08-14 20:56:24

尝尝这个,

代码语言:javascript
复制
<ComboBox x:Name="cbItems" ItemsSource="{Binding Items, Mode=TwoWay, 
            UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" 
            HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" 
            VerticalAlignment="Top" Width="120"/>


<ComboBox x:Name="cbPeices" ItemsSource="{Binding Items, Mode=TwoWay,
                UpdateSourceTrigger=PropertyChanged}" 
          DisplayMemberPath="Name" SelectedItem="{Binding ElementName=cbItems, Path=SelectedItem}"
            HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" 
            Width="120"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32018319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档