xaml style - how to make colors change on key

191 Views Asked by At

I have the brushes with different colors below.

<!-- SolidColorBrush -->
<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>

That I want to change to be red, for a negative button. I.e. not static, but switching them. Not at runtime, (at first) based on some value..

fx: int I = 0;

<!-- SolidColorBrush -->
<SolidColorBrush x:Key="Button.Static.Background" Color="Red"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="DarkRed/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="LightRed"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#Red"/>

I == 1

<!-- SolidColorBrush -->
<SolidColorBrush x:Key="Button.Static.Background" Color="Yellow"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="DarkYellow"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="LightYellow"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#Yellow"/>

I == 2

<!-- SolidColorBrush -->
<SolidColorBrush x:Key="Button.Static.Background" Color="Green"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="DarkGreen"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="LightGreen"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#Green"/>

There must be some smart way to set the default colors and not just being limited to one style of button... so I can bind the differnt colors used in the xaml to make different styles of buttons..

Anyone have an idea, feel free to make a comment..

1

There are 1 best solutions below

4
mm8 On

You can replace the resource dynamically:

Resources["Button.Static.Background"] = Brushes.Yellow;

For this to affect any element that currently references this resource, you need to reference it using the DynamicResource markup extension:

<TextBlock Text="Test" Foreground="{DynamicResource Button.Static.Background}" />

The default Button template does not use DynamicResource so you cannot change the colours of a button without modifying the template by for example replacing StaticResource with DynamicResource.