Marcel Grüger
Marcel Grüger

Reputation: 934

Set Style of ComboBoxItem = null, but programmatically

I have in my XAML the following lines as 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>

In my Window are several ComboBoxes where this is good. But I have one, where it is disturbing, so I wanted to set the style to null. I already put a Style="{x:Null}" inside the XAML-ComboBox. That gives the ComboBox itself a good view, but not the open Box (i.e. the ComboBoxItems). I use a DataBinding inside the Code-Behind, so how can I delete the window-style for the ComboBoxItems?

Upvotes: 0

Views: 537

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

You should add to ComboBox resources empty style with target type ComboBoxItem.

You can do this in the XAML like this:

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

Or you can do this in the code-behind using following code:

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

Upvotes: 1

Related Questions