Click here to Skip to main content
13,867,740 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 6 Jul 2017
Licenced CPOL

WPF Controls Color Customization

, 6 Jul 2017
Rate this:
Please Sign up or sign in to vote.
No more Template copies


Background property is a single extension point when it comes to control color scheme. Alternative colors (for mouse hover, selection, disabled state) are often hardcoded in control template. One has to make a Template copy to change them.

Recently, when I was working on WPF styles pack for my personal project (codename RetroUI), I wanted to make them reusable in the way that people who don't like my choice of default colors don't have to copy full control template. Additionally, I didn't want to create derived controls (e.g. CustomButton : Button) with an extended set of properties.

Using the Code

To avoid code duplication, I created reusable attached dependency properties: changing one style setter is simpler than copying a template. The set of attached properties includes pair of Background and Foreground brushes for Alternative, Selected and Disabled control states. The difference in their implementation is only in their class name.

public static class Alt
    #region Background
    public static readonly DependencyProperty BackgroundProperty =
              DependencyProperty.RegisterAttached("Background", typeof(Brush),
              typeof(Alt), new PropertyMetadata(null));

    public static Brush GetBackground(DependencyObject obj)
        return (Brush)obj.GetValue(Alt.BackgroundProperty);

    public static void SetBackground(DependencyObject obj, Brush value)
        obj.SetValue(Alt.BackgroundProperty, value);

    #region Foreground
    public static readonly DependencyProperty ForegroundProperty =
              DependencyProperty.RegisterAttached("Foreground", typeof(Brush),
              typeof(Alt), new PropertyMetadata(null));

    public static Brush GetForeground(DependencyObject obj)
        return (Brush)obj.GetValue(Alt.ForegroundProperty);

    public static void SetForeground(DependencyObject obj, Brush value)
        obj.SetValue(Alt.ForegroundProperty, value);

Then I created style setters for these properties and used them in a template in a theme resource dictionary:

<!-- default colors -->
<SolidColorBrush x:Key="RetroBlue"   Color="#00a0a0"/>
<SolidColorBrush x:Key="RetroGreen"  Color="#00a000"/>
<SolidColorBrush x:Key="RetroSilver" Color="#c0c0c0"/>

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource RetroGreen}"/>
    <Setter Property="retroUi:Alt.Background" Value="Black"/>
    <Setter Property="retroUi:Disabled.Background" Value="{StaticResource RetroSilver}"/>
    <Setter Property="BorderBrush" Value="{x:Null}"/>
    <Setter Property="Foreground"
    Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="4,2"/>
    <Setter Property="Template">
            <ControlTemplate TargetType="{x:Type Button}">
                        <ColumnDefinition Width="8" MaxWidth="8"/>
                        <ColumnDefinition Width="8" MaxWidth="8"/>
                        <RowDefinition Height="8"/>
                        <RowDefinition MinHeight="8"/>
                        <RowDefinition Height="8"/>
                    <Rectangle x:Name="PART_Shadow"
                               Fill="{Binding Path=(retroUi:Alt.Background),
                               RelativeSource={RelativeSource TemplatedParent}}"
                               Grid.Row="1" Grid.RowSpan="2"
                               Grid.Column="1" Grid.ColumnSpan="2"/>
                    <Border x:Name="border"
                                Grid.Row="0" Grid.RowSpan="2"
                                Grid.Column="0" Grid.ColumnSpan="2"
                                Background="{TemplateBinding Background}"
                        <ContentPresenter x:Name="contentPresenter" Focusable="False"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                Margin="{TemplateBinding Padding}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="border" Property="Grid.Column" Value="1"/>
                        <Setter TargetName="PART_Shadow" Property="Opacity" Value="0"/>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="border" Property="Background"
                                Value="{Binding Path=(retroUi:Disabled.Background),
                                RelativeSource={RelativeSource TemplatedParent}}"/>

Now I can chose different brushes for Background and Alt.Background, e.g.:

<UniformGrid Columns="1">
	<Button Content="Red" Margin="5"

	Background="Red" retroUi:Alt.Background="DarkRed"/>
	<Button Content="Green" Margin="5"

	Background="Green" retroUi:Alt.Background="DarkGreen"/>
	<Button Content="Blue" Foreground="White" Margin="5"

	Background="Blue" retroUi:Alt.Background="DarkBlue"/>



Thomas Levesque first described a solution with attached properties in his blog-post and used a term "parametrized styles". I have learnt that trick from him ("parametrized templates" sounds more precise in my opinion). My contribution here is a separate brush per control state (Alt.Background, Selected.Background, Disabled.Background) which can be widely applicable (even if some controls don't need all these brushes) and easily remembered.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

QuestionRetroUI Pin
D. Bernardo d'Annunzio7-Jul-17 17:39
memberD. Bernardo d'Annunzio7-Jul-17 17:39 
AnswerRe: RetroUI Pin
Alexander Sharykin7-Jul-17 23:23
memberAlexander Sharykin7-Jul-17 23:23 
QuestionWhy not just use a StaticResource as the color value? Pin
N.Mayer6-Jul-17 23:41
memberN.Mayer6-Jul-17 23:41 
AnswerRe: Why not just use a StaticResource as the color value? Pin
Alexander Sharykin7-Jul-17 3:48
memberAlexander Sharykin7-Jul-17 3:48 
GeneralRe: Why not just use a StaticResource as the color value? Pin
N.Mayer5-Jan-18 4:02
memberN.Mayer5-Jan-18 4:02 
AnswerRe: Why not just use a StaticResource as the color value? Pin
Alexander Sharykin7-Jul-17 4:08
memberAlexander Sharykin7-Jul-17 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 6 Jul 2017
Article Copyright 2017 by Alexander Sharykin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid