来自 C# 代码隐藏中的 xaml 等效项的自定义颜色

我在 wpf 应用程序中有一个带有网格的窗口。网格有一个十六进制背景值。我只想从代码后面检查该背景的值是否是我真正的意思。


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

在后面的代码中我得到:


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;

不得不提的是,MDark 是一个单选按钮。而且这个条件永远不会成立。我很感激你的帮助。:D


米琪卡哇伊
浏览 54回答 1
1回答

料青山看我应如是

您正在比较SolidColorBrush实例,它们显然不一样。而是比较实际的颜色值:var c = (Color) ColorConverter.ConvertFromString ("#424242");MainWindow mainWin = Application.Current.MainWindow as MainWindow;if (((SolidColorBrush) mainWin.GridMain.Background).Color == c)&nbsp;{&nbsp; &nbsp; MDark.IsChecked = true;}
打开App,查看更多内容
随时随地看视频慕课网APP