如何检查当前的背景色WPF C#

时间:2019-02-27 03:23:54

标签: c# wpf

如何检查按钮的当前颜色,这是到目前为止的代码

private void firstClick(object changer, RoutedEventArgs e)
        {
            Button x = (changer as Button);

            if (x backgroundcolor is blue)
            {

                x.Background = new SolidColorBrush(Colors.LightBlue);
                click++;

3 个答案:

答案 0 :(得分:1)

WPF颜色结构具有equality operator,因此您可以简单地编写以下代码:

\documentclass{beamer}

\usetheme{Darmstadt}
\usecolortheme{seahorse}

\title{text}

\begin{document}

\begin{frame}
\titlepage
\end{frame} 

\section{title}
\begin{frame}
    \begin{enumerate}
    \item test
    \end{enumerate}
\end{frame} 

\end{document}

答案 1 :(得分:0)

尝试一下:

        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++;
}

答案 2 :(得分:0)

由于您似乎难以理解其他答案(顺便说一句,它正确无误),因此使其变得简单 使用这个:

        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
        }