如何检查当前背景色wpf c#
我如何检查按钮的当前颜色这里是到目前为止的代码如何检查当前背景色wpf c#,c#,wpf,C#,Wpf,我如何检查按钮的当前颜色这里是到目前为止的代码 private void firstClick(object changer, RoutedEventArgs e) { Button x = (changer as Button); if (x backgroundcolor is blue) { x.Background = new SolidColorBrush(Col
private void firstClick(object changer, RoutedEventArgs e)
{
Button x = (changer as Button);
if (x backgroundcolor is blue)
{
x.Background = new SolidColorBrush(Colors.LightBlue);
click++;
试试这个:
public bool Equals(SolidColorBrush brush1, SolidColorBrush brush2) {
return brush1.Opacity == brush2.Opacity &&
brush1.Color.A == brush2.Color.A &&
brush1.Color.R == brush2.Color.R &&
brush1.Color.B == brush2.Color.B &&
brush1.Color.G == brush2.Color.G;
}
要获取颜色,请执行以下操作:
Color color1 = (Color)brush1.GetValue(SolidColorBrush.ColorProperty);
用法:
Button x = (changer as Button);
Brush blue = Brushes.Blue;
if (Equals(x.BackgroundColor,blue)) {
x.Background = new SolidColorBrush(Colors.LightBlue);
click++;
}
正如你对另一个答案(顺便说一句,它的核心)的质疑一样,让我们简单一点 使用以下命令:
var yourColor = System.Drawing.Color.Blue;
if ((x.Background as SolidColorBrush).Color.A == yourColor.A &&
(x.Background as SolidColorBrush).Color.R == yourColor.R &&
(x.Background as SolidColorBrush).Color.G == yourColor.G &&
(x.Background as SolidColorBrush).Color.B == yourColor.B)
{
//do something nice here
}
WPF颜色结构有一个,因此您可以简单地编写以下内容:
if (((SolidColorBrush)x.Background).Color == Colors.Blue)
{
...
}
我不明白