简体   繁体   English

如何根据我的第一个组合框的选择绑定以下列表框

[英]How to bind the following listboxes depending on the selection of my first combo box

I have 2 combo boxes. 我有2个组合框。 (combo1 and combo2) (combo1和combo2)
The first contains 2 options. 第一个包含2个选项。 car model and car wheels. 汽车模型和车轮。
The second contains 4 options. 第二个包含4个选项。 BMW, Mercedes, 4 wheels, 5 wheels. 宝马,奔驰,4轮,5轮。

I want the information for the second combo box to only show BMW and mercedes if combo box 1 is chosen as car model and only show 4 wheels and 5 wheels if combo box 1 is chosen as car wheels. 我希望第二个组合框的信息仅在选择组合框1作为汽车模型时显示BMW和奔驰,而在选择组合框1作为汽车车轮时仅显示4个车轮和5个车轮。

I am looking for a way to call the collection accordingly but unable to. 我正在寻找一种相应地调用集合但无法调用的方法。 Please advice how I can do this. 请提出建议,我该怎么做。 Thanks. 谢谢。

<ComboBox Name="combo1" Width="120">
                <ListBoxItem Name="box1" Content="Car Model"/> 
                <ListBoxItem Name="box2" Content="Car Wheels"/>
            </ComboBox>

<ComboBox Name="combo2" Width="120">
                <ListBoxItem Name="box3" Content="BMW"/> 
                <ListBoxItem Name="box4" Content="Mercedes"/>
                <ListBoxItem Name="box5" Content="4 Wheels"/> 
                <ListBoxItem Name="box6" Content="5 Wheels"/>
            </ComboBox>


private void Button_Click(object sender, RoutedEventArgs e)
{
    if (comboBox1.Text == "Car Model")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add(box3);
                comboBox2.Items.Add(box4);
            }
            else
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add(box5);
                comboBox2.Items.Add(box6);
            }
}

This doesn't work as I would need to click the button for this to happen. 这不起作用,因为我需要单击按钮才能发生这种情况。 I want it to occur as long as the first combobox(combo1) has a selected option. 我希望只要第一个combobox(combo1)具有选定的选项,它就会发生。

Also these listboxitems are automatically in a collection when I create thus I believe they are already stored in a form of array. 这些列表框项目在创建时也会自动保存在集合中,因此我相信它们已经以数组形式存储。 I am looking to know how I can access this array so I can loop through that instead of adding one by one. 我想知道如何访问此数组,以便可以遍历该数组,而不是一个接一个地添加。

Please advice how I can do these. 请建议我该怎么做。 Thanks. 谢谢。

First of all you should add ComboBoxItem and not ListBoxItem . 首先 ,您应该添加ComboBoxItem而不是ListBoxItem

Second , you can do all that in XAML, no need to use code behind. 其次 ,您可以在XAML中完成所有这些操作,而无需使用后面的代码。 Create array under resource section and set ItemsSource based on SelectedItem in first comboBox. 在资源部分下创建数组,并在第一个comboBox中基于SelectedItem设置ItemsSource。 This is how you do it XAML way: 这是XAML方法:

<StackPanel>
    <StackPanel.Resources>
        <x:Array Type="ComboBoxItem" x:Key="ModelsArray">
            <ComboBoxItem Name="box3" Content="BMW"/>
            <ComboBoxItem Name="box4" Content="Mercedes"/>
        </x:Array>
        <x:Array Type="ComboBoxItem" x:Key="WheelsArray">
            <ComboBoxItem Name="box5" Content="4 Wheels"/>
            <ComboBoxItem Name="box6" Content="5 Wheels"/>
        </x:Array>
    </StackPanel.Resources>
    <ComboBox Name="combo1" Width="120">
        <ComboBoxItem Name="box1" Content="Car Model"/>
        <ComboBoxItem Name="box2" Content="Car Wheels"/>
    </ComboBox>

    <ComboBox Name="combo2" Width="120">
        <ComboBox.Style>
            <Style TargetType="ComboBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedItem.Content, 
                                        ElementName=combo1}" Value="Car Model">
                        <Setter Property="ItemsSource"
                                Value="{StaticResource ModelsArray}"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding SelectedItem.Content, 
                                       ElementName=combo1}" Value="Car Wheels">
                        <Setter Property="ItemsSource"
                                Value="{StaticResource WheelsArray}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
    </ComboBox>
</StackPanel>

OR 要么

If you still want to do it in code behind, hook SelectionChanged event. 如果仍要在后面的代码中执行此操作,请钩挂SelectionChanged事件。

<ComboBox Name="combo1" Width="120" SelectionChanged="ComboBox_SelectionChanged">
    <ComboBoxItem Name="box1" Content="Car Model"/> 
    <ComboBoxItem Name="box2" Content="Car Wheels"/>
</ComboBox>
<ComboBox Name="combo2" Width="120"/>

Code behind : Code behind

private void ComboBox_SelectionChanged(object sender,
                                       SelectionChangedEventArgs e)
{
   if (((ComboBoxItem)combo1.SelectedItem).Content.ToString() == "Car Model")
   {
      combo2.Items.Clear();
      combo2.Items.Add("BMW");
      combo2.Items.Add("Mercedes");
   }
   else
   {
      combo2.Items.Clear();
      combo2.Items.Add("4 Wheels");
      combo2.Items.Add("5 Wheels");
   }
}

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

相关问题 如何将组合框列表和选择绑定到动态列表? - How to bind a combo box list and selection to a dynamic list? 根据数据网格视图选择绑定到组合框 - Bind to Combo box based on Data Gridview selection C#Windows窗体如何根据第一个组合框中的选择更改第二个组合框的值 - C# Windows Forms how to change values of second combo box based on selection in first combo box 根据组合框 c# 的选择,我的代码没有切换到所需的 function - My code is not switching to the desired function depending upon the selection from combo box c# 如何将boolean绑定到组合框 - How to bind boolean to combo box 如何在Silverlight中将折线图与组合框绑定? - How to bind Line Charts with Combo box in silverlight? 如何将数据绑定到Telerik muticolumn组合框? - How to bind data to telerik muticolumn combo box? WPF应用程序中第一个组合框选择更改时将数据填充到第二个组合框 - populating data to second combo box on first combo box selection change in wpf application 如何将 object 的列表属性绑定到组合框子菜单? - How can I bind my list Property of the object to combo box submenus? 如何在当前组合框中选择值时显示另一个组合框? - How to make visible another combo box on selection of a value in current combo box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM