-1

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..

jamesnet214
  • 1,044
  • 13
  • 21
kfn
  • 620
  • 1
  • 7
  • 26

1 Answers1

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I am thinking, what I really would love, was to have use a key, with a directory of colors. i.e. red, green,yellow. and then chaning with the groups of colors.. – kfn May 18 '21 at 07:28
  • You could put all related resources in a `ResourceDictonary` and then replace this one with another one to change colours. – mm8 May 18 '21 at 11:35
  • That is exactly what I did, and the right way to do it (I think) – kfn May 18 '21 at 13:14
  • @kfn: So what's your remaining question? – mm8 May 18 '21 at 14:52