简体   繁体   English

来自Xaml的自定义颜色(与C#代码相同)

[英]Custom Color from xaml equivalent in C# code-behind

I have a window in a wpf application which has a grid. 我在具有网格的wpf应用程序中有一个窗口。 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. 我不得不提到MDark是radioButton。 And the condition never gets true. 而且条件永远不会成立。 I appreciate the help. 感谢您的帮助。 :D :D

You're comparing SolidColorBrush instances, which are obviously not the same. 您正在比较SolidColorBrush实例,这显然是不同的。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM