我如何检查按钮的当前颜色,这是到目前为止的代码
private void firstClick(object changer, RoutedEventArgs e)
{
Button x = (changer as Button);
if (x backgroundcolor is blue)
{
x.Background = new SolidColorBrush(Colors.LightBlue);
click++;
WPF颜色结构具有等效操作员,因此您可以简单地写下:
if (((SolidColorBrush)x.Background).Color == Colors.Blue)
{
...
}
尝试以下:
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
}