我刚刚学会了如何将ComboBox绑定到ObservableCollection。喔呼!是否有方法将第二个ComboBox绑定到第一个ComboBox的选定集合?因此,每个项目都有一个ObservableCollection的Peices。当您选择一个项目时,我希望第二个ComboBox显示所选项目的代码!
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集合的内容。
发布于 2015-08-14 20:56:50
绑定到第一个组合框的选定项,如下所示:
<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>发布于 2015-08-14 20:43:06
只需将两者绑定到相同的事物,但添加双向绑定模式。所以:
<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>发布于 2015-08-14 20:56:24
尝尝这个,
<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"/>https://stackoverflow.com/questions/32018319
复制相似问题