Matt McManis Matt McManis - 1 year ago 122
C# Question

XAML 2 ComboBoxes Color Styles with Resource Dictionary?

With the help of @ebattulga this has been solved.

I created a Resource Dictionary with multiple template overrides.

And an example visual studio project to download

The following keys have Red and Blue added to the end of name:

  • ComboBoxToggleButton

  • ComboBoxTemplate

  • ComboBoxEditableTemplate

ComboBox Red & Blue

I have 2 ComboBoxes. I need to make one Red and one Blue.

I've made an example project below.

Changing the background color in Brush Properties doesn't work. You need to override the Default Template Style LinearGradientBrush ComboBox.Static.Background which is grey and white.

Using Edit Template I can override the Default brush to change them all Red. But I cannot find a way to create another Style to make the other Blue.

I'm trying Resource Dictionary, but the Style does not take effect.


XAML 2 ComboBoxes

<Window x:Class="ComboBoxColors.MainWindow"
Title="MainWindow" Height="350" Width="525">

<ResourceDictionary Source="ComboBoxStylesDictionary.xaml"/>

<ComboBox x:Name="ComboBoxRed" Style="{StaticResource ComboBoxRed}" HorizontalAlignment="Left" Margin="109,105,0,0" VerticalAlignment="Top" Width="120"/>
<ComboBox x:Name="ComboBoxBlue" Style="{StaticResource ComboBoxBlue}" HorizontalAlignment="Left" Margin="286,105,0,0" VerticalAlignment="Top" Width="120"/>

Resource Dictionary

<ResourceDictionary xmlns=""

<LinearGradientBrush x:Key="ComboBoxRed.Static.Background" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0.0"/>
<GradientStop Color="Red" Offset="1.0"/>
<SolidColorBrush x:Key="ComboBoxRed.Static.Border" Color="Red"/>
<Style x:Key="ComboBoxRed" TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="{StaticResource ComboBoxRed.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource ComboBoxRed.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>

<LinearGradientBrush x:Key="ComboBoxBlue.Static.Background" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="Blue" Offset="0.0"/>
<GradientStop Color="Blue" Offset="1.0"/>
<SolidColorBrush x:Key="ComboBoxBlue.Static.Border" Color="Blue"/>
<Style x:Key="ComboBoxBlue" TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="{StaticResource ComboBoxBlue.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource ComboBoxBlue.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>



I have also tried this, but resource not found.

Brush ComboBoxRedStyle = (Brush)Application.Current.FindResource("ComboBoxRed");
ComboBoxRed.Background = ComboBoxRedStyle;

And this with no effect.

ComboBoxRed.Background = Brushes.Red;

Answer Source

You declare resource in App.xaml

    <ResourceDictionary Source="ComboBoxStylesDictionary.xaml"/>

Or if you only use it in locally, use this

Style ComboBoxRedStyle = (Style)this.FindResource("ComboBoxRed");