基于组合框项目选择的组合框

时间:2016-08-09 05:21:40

标签: wpf

我有一个包含四个项目的组合框。

如果我选择第一个项目,则第二个组合框必须显示与第一个组合框项目的选择有关的项目。如果我选择第二个项目,那么第二个组合框必须显示一些项目。

请提出一些建议。

1 个答案:

答案 0 :(得分:0)

在XAML文件中添加2个组合框:

    <ComboBox Name="cbTest1" SelectionChanged="cbTest1_SelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" >
        <ComboBoxItem Content="1"></ComboBoxItem>
        <ComboBoxItem Content="2"></ComboBoxItem>
        <ComboBoxItem Content="3"></ComboBoxItem>
        <ComboBoxItem Content="4"></ComboBoxItem>
    </ComboBox>

    <ComboBox Name="cbTest2" ItemsSource="{Binding Data}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" />

在函数cbTest1_SelectionChanged中,根据所选值,您可以在变量数据中设置值

  private void cbTest1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        List<string> oData = new List<string>();
        if (((ComboBoxItem)cbTest1.SelectedValue).Content.ToString() == "1")
        {                
            oData.Add("DataType1_1");
            oData.Add("DataType1_2");
            oData.Add("DataType1_3");
            oData.Add("DataType1_4");                
        }
        else if (((ComboBoxItem)cbTest1.SelectedValue).Content.ToString() == "2")
        {                
            oData.Add("DataType2_1");
            oData.Add("DataType2_2");
            oData.Add("DataType2_3");
            oData.Add("DataType2_4");                
        }

        viewModel.Data = oData;
    }