[英]Custom Color from xaml equivalent in C# code-behind
我在具有網格的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是radioButton。 而且條件永遠不會成立。 感謝您的幫助。 :D
您正在比較SolidColorBrush
實例,這顯然是不同的。 比較實際的顏色值:
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.