我剛剛學會了如何將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集合的內容。
當我必須告訴它數據在哪裏?哈哈謝謝! = P – GFocus