我刚刚学会了如何将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