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

本文关键字:绑定 选择 列表 我的 第一个 组合 何根 | 更新日期: 2023-09-27 17:58:11

我有两个组合框。(combo1和combo2)
第一个包含2个选项。汽车模型和车轮
第二个包含4个选项。宝马,奔驰,4轮,5轮。

如果选择组合框1作为车型,我希望第二个组合框的信息仅显示宝马和梅赛德斯,如果选择组合盒1作为车轮,则仅显示4个车轮和5个车轮。

我正在寻找一种方法来相应地调用集合,但无法。请建议我如何做到这一点。谢谢

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

这不起作用,因为我需要点击按钮才能实现。只要第一个组合框(combo1)有一个选定的选项,我就希望它发生。

此外,当我创建时,这些列表项会自动存储在集合中,因此我相信它们已经以数组的形式存储。我想知道如何访问这个数组,这样我就可以循环使用它,而不是逐个添加。

请告诉我如何做这些。谢谢

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

首先您应该添加ComboBoxItem,而不是ListBoxItem

其次,您可以在XAML中完成所有这些操作,而无需使用代码隐藏。在resource部分下创建数组,并根据第一个comboBox中的SelectedItem设置ItemsSource。以下是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>

如果您仍然想在代码隐藏中执行,请挂接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

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