如何通过将另一个 class/ 传递给 WPF 中的函数来更新按钮的颜色

How to update the Color of Button from another class/ by passing it in a function in WPF

我想通过另一个 class/by 在函数中传递按钮来更改特定条件下按钮的颜色。我有 Winform 的代码,但我需要它用于 WPF。 这是 Winform 应用程序代码

internal static void BtnOnOff(Button button1, byte v)
{
    if (v == 1)
    {
        button1.BackColor = System.Drawing.Color.YellowGreen; // What for WPF ?
    }
    else
    {
        button1.BackColor = System.Drawing.SystemColors.Control;
    }
}

我假设 BackColor 你的意思是 Background prop

button1.Background = Brushes.YellowGreen

请注意,您必须更换画笔。试试这个:

internal static void BtnOnOff(Button button1, byte v){
 if (v==1)
 {
 button1.Background= System.Windows.Media.Brushes.YellowGreen;
 }
 else
 {
 button1.Background= System.Windows.SystemColors.ControlBrush;
 }
}