将组合框绑定到WPF中的另一个组合框 [英] binding combobox to another combobox in wpf
本文介绍了将组合框绑定到WPF中的另一个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在wpf中有两个组合框,其中一个组合框看起来像这样:
I have two combo boxes in wpf one of the combo boxes looks like so:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="244,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="Peugeut" />
<ComboBoxItem Content="Ford" />
<ComboBoxItem Content="BMW" />
</ComboBox>
我想知道如何将第二个combobox2绑定到特定汽车制造商列表中,并将其组合到combobox1中的选定项目.
I was wondering how you bind the second combobox2 to list specifc car makes to the selected item in combobox1.
如果选择了Peurgeut,则在组合框2中应该有一个列表:
If Peurgeut is selected then in combobox two there should be a list:
106
206
306
或如果选择了宝马,则
4 series
5 series
依此类推
推荐答案
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<ComboBox Height="23" ItemsSource="{Binding Cars}" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="244,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"/>
<ComboBox Height="23" Grid.Row="1" ItemsSource="{Binding SelectedItem.Series, ElementName=comboBox1}" HorizontalAlignment="Left" Margin="244,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Cars = new ObservableCollection<Car>();
Cars.Add(new Car() { Name = "Peugeut", Series = new ObservableCollection<string>() { "106", "206", "306" } });
Cars.Add(new Car() { Name = "Ford", Series = new ObservableCollection<string>() { "406", "506", "606" } });
Cars.Add(new Car() { Name = "BMW", Series = new ObservableCollection<string>() { "706", "806", "906" } });
DataContext = this;
}
public ObservableCollection<Car> Cars { get; set; }
}
public class Car
{
public string Name { get; set; }
public ObservableCollection<string> Series { get; set; }
}
我希望这会有所帮助.
这篇关于将组合框绑定到WPF中的另一个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文