简体   繁体   English

将组合框与wpf中的另一个组合框结合

[英]binding combobox to another combobox in wpf

I have two combo boxes in wpf one of the combo boxes looks like so: 我在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>

I was wondering how you bind the second combobox2 to list specifc car makes to the selected item in combobox1. 我想知道如何绑定第二个combobox2以列出特定于carobox1中所选项目的carc。

If Peurgeut is selected then in combobox two there should be a list: 如果Peurgeut被选中,那么在组合框2中应该有一个列表:

106
206
306 

or if bmw is selected then 或者如果选择了宝马那么

4 series
5 series

And so on 等等

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

I hope this will help. 我希望这将有所帮助。

Unless you look up the data I don't think you can do it with just XAML. 除非您查看数据,否则我认为您不能仅使用XAML。 If, however, you created a class to bind your combo boxes you could have a class with something like: 但是,如果您创建了一个用于绑定组合框的类,则可以使用类似于以下内容的类:

public class CarMake
{
    public string Make {get; set;}
    public List<string> Models {get; set;}
}

Then in your first combo box, just bind to an instance of List with the info populated, then bind the second combo box like: 然后在你的第一个组合框中,只需绑定一个List实例并填充信息,然后绑定第二个组合框,如:

<ComboBox ItemsSource="{Binding ElementName=FirstComboBox, Path=SelectedItem.Models}" ></ComboBox>

That should get you going... 这应该让你去...

Try to add the items in box2 programmtically when ComboBox1 item is selected by user. 当用户选择ComboBox1项时,尝试以编程方式添加box2中的项目。

        if (combobox1.SelectedText == "Peurgeut")
        {
            box2.Items.Add("106");
            box2.Items.Add("206");
            box2.Items.Add("306");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM