为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
米琪卡哇伊 2023-07-09 09:52:07
我在 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
查看完整描述

1 回答

?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

您正在比较SolidColorBrush实例,它们显然不一样。而是比较实际的颜色值:


var c = (Color) ColorConverter.ConvertFromString ("#424242");

MainWindow mainWin = Application.Current.MainWindow as MainWindow;

if (((SolidColorBrush) mainWin.GridMain.Background).Color == c) 

{

    MDark.IsChecked = true;

}


查看完整回答
反对 回复 2023-07-09
  • 1 回答
  • 0 关注
  • 51 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信