2015-08-14 101 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"/> 
相關問題