Amir Esmaeili
Amir Esmaeili

Reputation: 88

Custom Color from xaml equivalent in C# code-behind

I have a window in a wpf application which has a grid. The Grid has a value for it's background in hex. I just want to check from code behind if the value of that background is what I really meant.

<Grid Background="#424242" Name="GridMain">

And in the code behind I got :

SolidColorBrush a = new SolidColorBrush();
var b = (SolidColorBrush)new BrushConverter().ConvertFrom("#424242");
MainWindow mainWin = Application.Current.MainWindow as MainWindow;
if (mainWin.GridMain.Background ==  b)
     MDark.IsChecked = true;

I have to mention that MDark is a radioButton. And the condition never gets true. I appreciate the help. :D

Upvotes: 0

Views: 850

Answers (1)

Lennart
Lennart

Reputation: 10354

You're comparing SolidColorBrush instances, which are obviously not the same. Compare the actual color value instead:

var c = (Color) ColorConverter.ConvertFromString ("#424242");
MainWindow mainWin = Application.Current.MainWindow as MainWindow;
if (((SolidColorBrush) mainWin.GridMain.Background).Color == c) 
{
    MDark.IsChecked = true;
}

Upvotes: 4

Related Questions