多选ComboBox,WPF

时间:2011-08-09 08:33:53

标签: c# wpf combobox

我希望我的comboBox包含单选按钮,因为它是用于多选的comboBoxItems。 Xaml就是:

<ComboBox Background="Transparent" x:FieldModifier="public" Margin="0,0,0,0" x:Name="comboBox_Errors" FontSize="11"  MinHeight="19" Height="Auto" MinWidth="150" BorderThickness="1">
                                <ComboBox.ItemTemplate>
                                    <HierarchicalDataTemplate>
                                        <RadioButton />
                                    </HierarchicalDataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>

这是尝试这个comboBox的代码:

            ComboBoxItem c1 = new ComboBoxItem();
            c1.Content = "Error 1";
            this.DataControl_PalcesDataControl.comboBox_Errors.Items.Add(c1);
            ComboBoxItem c2 = new ComboBoxItem();
            c2.Content = "Error 2";
            this.DataControl_PalcesDataControl.comboBox_Errors.Items.Add(c2);
            ComboBoxItem c3 = new ComboBoxItem();
            c3.Content = "Error 3";
            this.DataControl_PalcesDataControl.comboBox_Errors.Items.Add(c3);
            ComboBoxItem c4 = new ComboBoxItem();
            c4.Content = "Error 4";
            this.DataControl_PalcesDataControl.comboBox_Errors.Items.Add(c4);
            ComboBoxItem c5 = new ComboBoxItem();
            c5.Content = "Error 5";
            this.DataControl_PalcesDataControl.comboBox_Errors.Items.Add(c5);

当我运行应用程序时,comboBox中没有任何内容......问题是什么? 比你。

1 个答案:

答案 0 :(得分:1)

无需创建ComboBoxItem,只需将字符串添加到comboBox_Errors:

comboBox_Errors.Items.Add("Error 1");

另外,你需要让RadioButton展示一些东西,这可能是最简单的方法:

<RadioButton Content="{Binding}" />