WPF Styling Colors

AGoodDisplayName picture AGoodDisplayName · Mar 12, 2012 · Viewed 14.1k times · Source

I want to do something like this:

Resource Dictionary

<Color x:Key="clrPrimary">#5381ac</Color>
<Color x:Key="clrSecondary">#20558a</Color>

<Style TargetType="Grid" x:Key="myGrid">
    <Setter Property="Background" Value="{StaticResource clrPrimary"/>
</Style>

Getting Exception:

'#FF5381AC' is not a valid value for property 'Background'.

Having trouble nailing it down, can any one point me in the right direction?

Answer

Matt Hamilton picture Matt Hamilton · Mar 12, 2012

Background is a Brush, not a Color. Your best bet is to define your "Primary" and "Secondary" resources as brushes rather than colours.

Pretty sure you could even base the brushes off your existing colours.

<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource clrPrimary}" />
...
    <Setter Property="Background" Value="{StaticResource PrimaryBrush}" />