我在wpf中有两个组合框,其中一个组合框看起来像这样:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="244,10,0" Name="comboBox2" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Peugeut" /> <ComboBoxItem Content="Ford" /> <ComboBoxItem Content="BMW" /> </ComboBox>
我想知道如何绑定第二个comboBox2以列出特定于caroBox1中所选项目的carc.
如果Peurgeut被选中,那么在组合框2中应该有一个列表:
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,0" Name="comboBox1" VerticalAlignment="Top" Width="120"/> <ComboBox Height="23" Grid.Row="1" ItemsSource="{Binding SelectedItem.Series,ElementName=comboBox1}" HorizontalAlignment="Left" Margin="244,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; } }
我希望这将有所帮助.