Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a hex code to brushes on C# WPF?

I use this code to change my background t1.Background = Brushes.White; and it's working but how do I do it with hex codes? Because t1.Background = Brushes.#FF2463AE; doesn't work. I'm using Visual Studio 2015. Thank You.

like image 836
Carl Dun Avatar asked Sep 06 '25 03:09

Carl Dun


2 Answers

Use BrushConverter

t1.Background = (Brush)(new BrushConverter().ConvertFrom("#FF2463AE"));

It would probably be best to create a single instance of the converter if you are doing many conversions.

like image 147
Cyral Avatar answered Sep 08 '25 18:09

Cyral


You could try it like this:

SolidColorBrush color = (SolidColorBrush)(new BrushConverter().ConvertFrom("#ffaacc"));
t1.Background = color;
like image 20
McBooley Avatar answered Sep 08 '25 17:09

McBooley