Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set background color because of HTML Hex code

Tags:

wpf

I am trying to set the background of a template by using color I found online in HEX code:

<Color x:Key="BaseColor">#408DD2</Color>

....

<ControlTemplate.Triggers>
     <Trigger Property="IsSelected" Value="True">
          <Setter TargetName="Border" Property="Background" 
                Value="{StaticResource BaseColor}" />
     </Trigger>
</ControlTemplate.Triggers>

The error I get is:

'#FF408DD2' is not a valid value for property 'Background'.
like image 979
Bill Software Engineer Avatar asked Feb 14 '23 05:02

Bill Software Engineer


2 Answers

Border.Background is of type System.Windows.Media.Brush, you cannot assign a System.Windows.Media.Color to that.

Instead, create a resource of type Brush:

<SolidColorBrush x:Key="BaseColor" Color="#408DD2"/>

or,

have your Setter create the Brush to be assigned to that property:

 <Setter TargetName="Border" Property="Background">
     <Setter.Value>
        <SolidColorBrush Color="{StaticResource BaseColor}"/>
     </Setter.Value>
 </Setter>
like image 101
Federico Berasategui Avatar answered May 04 '23 20:05

Federico Berasategui


Background is of type Brush and not Color. So you need to provide Brush to it:

<SolidColorBrush x:Key="BaseColor" Color="#408DD2"/>
like image 33
Rohit Vats Avatar answered May 04 '23 19:05

Rohit Vats