Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SolidColorBrush color change at runtime

Tags:

wpf

c#-4.0

xaml

I have in my application resources file brush:

<SolidColorBrush x:Key="MainColor" Color="#FF15428B" /> 

I want to change color of this brush at runtime. I added color picker - when user choose color I want this brush to have selected color.

I tried code like that:

SolidColorBrush MainColor = new SolidColorBrush(SelectedColor);

But it didn't work.

like image 255
Marta Avatar asked Dec 12 '22 12:12

Marta


2 Answers

You need to set the existing brush's Color property.

You can get the instance by writing (SolidColorBrush)Resources["MainColor"]

like image 168
SLaks Avatar answered Jan 05 '23 12:01

SLaks


You can access Resources from the code-behind with the TryFindResource method:

SolidColorBrush myBrush = (SolidColorBrush)this.TryFindResource("myBrush");

if (myBrush != null)
{
    myBrush.Color = Colors.Yellow  ;
}
like image 31
fixagon Avatar answered Jan 05 '23 12:01

fixagon