WPF Styling Colors
Asked Answered
L

3

16

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?

Lemberg answered 12/3, 2012 at 21:34 Comment(0)
L
21

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}" />
Letty answered 12/3, 2012 at 21:40 Comment(0)
A
6

The background property needs a brush to work.

<Window.Resources>
    <SolidColorBrush x:Key="clrPrimary" Color="#5381ac" />
</Window.Resources>
Aragon answered 12/3, 2012 at 21:41 Comment(0)
U
1

The Background Property is of type System.Windows.Media.Brush, not a Color.

Uneventful answered 12/3, 2012 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.