如何比较if((sender作为Grid).背景== new SolidColorBrush(Colors.Green)
本文关键字:new 背景 SolidColorBrush Green Colors Grid sender 何比较 比较 if 作为 | 更新日期: 2023-09-27 18:17:19
如何比较if((sender as Grid)。Background== new SolidColorBrush(Colors.Green)) in wpf
网格是动态的
下面是代码
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
System.Windows.Media.Brush newColor = System.Windows.Media.Brushes.Yellow;
// SolidColorBrush newBrush = (SolidColorBrush)newColor;
// // System.Drawing.Brush b = new System.Drawing.SolidBrush((System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString(new System.Windows.Media.BrushConverter().ConvertToString(Colors.Yellow)));
//// System.Windows.Media.Color imageColor =( System.Windows.Media.Color) newBrush;
string co = null;
if((sender as Grid).Background== new SolidColorBrush(Colors.Green))
co = "Audited";
else if((sender as Grid).Background== new SolidColorBrush(Colors.Red))
co = "DoNotAudit";
else if((sender as Grid).Background== new SolidColorBrush(Colors.Orange))
co = "ReAudit";
else if((sender as Grid).Background== new SolidColorBrush(Colors.Yellow))
co = "TobeAudited";
MessageBox.Show(co);
}
}
co显示空值
你不应该比较两种不同的画笔,相反,把两种颜色都拿出来比较:
var grid = sender as Grid;
if(grid != null)
{
var background = grid.Background as SolidColorBrush;
if(background != null)
{
var color = background.Color;
if(Colors.Green.Equals(color))
{
co = "Audited";
}
else if(Colors.Red.Equals(color))
{
co = "DoNotAudit";
}
else if(Colors.Orange.Equals(color))
{
co = "ReAudit";
}
else if(Colors.Yellow.Equals(color))
{
co = "TobeAudited";
}
}
}
你的代码暗示你没有使用MVVM作为模式。WPF应该使用MVVM模式进行编程。你可能想要查找并使用它,它使事情变得容易得多。