WPF Styling Colors

14,866

Solution 1

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}" />

Solution 2

The background property needs a brush to work.

<Window.Resources>
    <SolidColorBrush x:Key="clrPrimary" Color="#5381ac" />
</Window.Resources>

Solution 3

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

Share:
14,866
AGoodDisplayName
Author by

AGoodDisplayName

Updated on June 14, 2022

Comments

  • AGoodDisplayName
    AGoodDisplayName about 2 years

    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?