设置ComboBoxItem的样式= null,但是以编程方式

时间:2015-02-25 13:09:27

标签: c# wpf xaml combobox

我在我的XAML中有以下几行作为Window.Resources:

    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush ImageSource="pics/greenbutton.png" />
            </Setter.Value>
        </Setter>
        <Setter Property="Foreground" Value="White" />
    </Style>

在我的窗口中有几个ComboBox,这是好的。但是我有一个令人不安的地方,所以我想将样式设置为null。我已经在XAML-ComboBox中放了一个Style="{x:Null}"。这给了ComboBox本身一个很好的视图,但不是打开的Box(即ComboBoxItems)。我在Code-Behind中使用DataBinding,那么如何删除ComboBoxItems的窗口样式?

1 个答案:

答案 0 :(得分:1)

您应该将ComboBox资源添加到目标类型为ComboBoxItem的空样式。

您可以在XAML中执行此操作:

<ComboBox x:Name="myComboBox" ...>
    <ComboBox.Resources>
        <Style TargetType="ComboBoxItem">
        </Style>
    </ComboBox.Resources>
 ...
</ComboBox>

或者您可以使用以下代码在代码隐藏中执行此操作:

myComboBox.Resources.Add(typeof(ComboBoxItem), new Style(typeof(ComboBoxItem)));