Irakli Lekishvili Irakli Lekishvili - 3 months ago 28
C# Question

How to convert color code into media.brush?

I have a rectangle that i want to fill with a color. When i write

Fill = "#FFFFFF90"
it shows me an error:


Cannot implicitly convert type 'string' to 'System.Windows.Media.Brush


Please give me some advice.

Answer

You could use the same mechanism the XAML reading system uses: Type converters

var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush)converter.ConvertFromString("#FFFFFF90");
Fill = brush;