Matt McManis
Matt McManis

Reputation: 4675

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. http://pastebin.com/nt3FxkM4

And an example visual studio project to download https://www.dropbox.com/s/20mpnbi27xv7nny/ComboBoxColors.zip?dl=0

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

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.

ComboBoxes

XAML 2 ComboBoxes

<Window x:Class="ComboBoxColors.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary Source="ComboBoxStylesDictionary.xaml"/>
    </Window.Resources>

    <Grid>
        <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"/>
    </Grid>
</Window>

Resource Dictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <LinearGradientBrush x:Key="ComboBoxRed.Static.Background" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="Red" Offset="0.0"/>
        <GradientStop Color="Red" Offset="1.0"/>
    </LinearGradientBrush>
    <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}}"/>
    </Style>

    <LinearGradientBrush x:Key="ComboBoxBlue.Static.Background" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="Blue" Offset="0.0"/>
        <GradientStop Color="Blue" Offset="1.0"/>
    </LinearGradientBrush>
    <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}}"/>
    </Style>

</ResourceDictionary>

C#

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;

Upvotes: 2

Views: 1041

Answers (2)

ebattulga
ebattulga

Reputation: 10981

You declare resource in App.xaml

<Application.Resources>
    <ResourceDictionary Source="ComboBoxStylesDictionary.xaml"/>
</Application.Resources>

Or if you only use it in locally, use this

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

Upvotes: 2

Shukant Pal
Shukant Pal

Reputation: 730

In XAML, use it like this,

<Page>
    <Page.Resources>
        <ResourceDictionary Source="GiveFileName"/>
    </Page.Resources>
</Page>

Upvotes: 0

Related Questions