2015-08-14 103 views
1

我刚刚学会了如何将ComboBox绑定到ObservableCollection。呜呼!有没有办法将第二个ComboBox绑定到第一个ComboBox的选定集合?所以每个Item都有一个Peser的ObservableCollection。当你选择一个项目时,我想让第二个组合框显示选中的项目的Peices!在ObservableCollection中绑定ObservableCollection

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> 

更新:好了,项目是 '项目' 的清单。项目有一个'Peice'的列表。我希望组合框2显示所选项目的Peices集合的内容。

回答

2

结合第一组合框的选择项,像这样:

<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> 
+0

当我必须告诉它数据在哪里?哈哈谢谢! = P – GFocus

0

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

<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

这些都连接到外部的ObservableCollection项目。一个Item类有一个名为Peices的ObservableCollection,加载了一个名为Peice的类。当我选择一个项目时,我希望第二个组合框加载所选项目的peices集合。 – GFocus

0

试试这个,

<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"/> 
相关问题