gpt4 book ai didi

c# - 将组合框绑定(bind)到 wpf 中的另一个组合框

转载 作者:太空狗 更新时间:2023-10-29 14:56:12 26 4
gpt4 key购买 nike

我在 wpf 中有两个组合框,其中一个组合框如下所示:

            <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>

我想知道您如何绑定(bind)第二个组合框 2 以列出组合框 1 中所选项目的特定汽车制造商。

如果选择了 Peurgeut,那么在组合框二中应该有一个列表:

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; }
}

希望对您有所帮助。

关于c# - 将组合框绑定(bind)到 wpf 中的另一个组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com