改变按钮的背景颜色

时间:2022-11-20 11:28:12

Using C# and Visual Studio 2010, how can I change the background color of a button once another button is pressed? Am I not including a System.? wrong?

使用c#和Visual Studio 2010,当另一个按钮被按下时,如何更改按钮的背景颜色?我不包括一个系统吗?错了吗?

What I have at the moment is:

我现在所拥有的是:

ButtonToday.Background = Color.Red;

ButtonToday。背景= Color.Red;

And it's not working.

这并不是工作。

6 个解决方案

#1


55  

WinForm:

WinForm上:

private void button1_Click(object sender, EventArgs e)
{
   button2.BackColor = Color.Red;
}

WPF:

WPF:

private void button1_Click(object sender, RoutedEventArgs e)
{
   button2.Background = Brushes.Blue;
}

#2


12  

In WPF, the background is not a Color, it is a Brush. So, try this for starters:

在WPF中,背景不是颜色,而是画笔。所以,首先试试这个:

using System.Windows.Media;

// ....

ButtonToday.Background = new SolidColorBrush(Colors.Red);

More sensibly, though, you should probably look at doing this in your Xaml instead of in code.

但是,更明智的是,您可能应该考虑在Xaml中而不是在代码中这样做。

#3


4  

Code for set background color, for SolidColor:

设置背景色代码,设置为单色:

button.Background = new SolidColorBrush(Color.FromArgb(Avalue, rValue, gValue, bValue));

#4


1  

I had trouble at first with setting the colors for a WPF applications controls. It appears it does not include System.Windows.Media by default but does include Windows.UI.Xaml.Media, which has some pre-filled colors.

我最初在设置WPF应用程序控件的颜色时遇到了麻烦。它似乎不包含System.Windows。媒体默认包括Windows.UI.Xaml。媒体,它有一些预先填充的颜色。

I ended up using the following line of code to get it to work:

最后我用了下面一行代码让它工作:

grid.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.CadetBlue);

You should be able to change grid.Background to most other controls and then change CadetBlue to any of the other colors it provides.

您应该能够更改网格。背景到大多数其他控件,然后将CadetBlue更改为它提供的任何其他颜色。

#5


1  

// WPF

// Defined Color
button1.Background = Brushes.Green;

// Color from RGB
button2.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));

#6


0  

I doubt if any of those should work. Try: First import the namespace in the beginning of the code page as below.

我怀疑这其中的任何一个是否有效。Try:首先在代码页的开头导入名称空间,如下所示。

using System.Drawing;

then in the code.

然后在代码中。

Button4.BackColor = Color.LawnGreen;

Hope it helps.

希望它可以帮助。

#1


55  

WinForm:

WinForm上:

private void button1_Click(object sender, EventArgs e)
{
   button2.BackColor = Color.Red;
}

WPF:

WPF:

private void button1_Click(object sender, RoutedEventArgs e)
{
   button2.Background = Brushes.Blue;
}

#2


12  

In WPF, the background is not a Color, it is a Brush. So, try this for starters:

在WPF中,背景不是颜色,而是画笔。所以,首先试试这个:

using System.Windows.Media;

// ....

ButtonToday.Background = new SolidColorBrush(Colors.Red);

More sensibly, though, you should probably look at doing this in your Xaml instead of in code.

但是,更明智的是,您可能应该考虑在Xaml中而不是在代码中这样做。

#3


4  

Code for set background color, for SolidColor:

设置背景色代码,设置为单色:

button.Background = new SolidColorBrush(Color.FromArgb(Avalue, rValue, gValue, bValue));

#4


1  

I had trouble at first with setting the colors for a WPF applications controls. It appears it does not include System.Windows.Media by default but does include Windows.UI.Xaml.Media, which has some pre-filled colors.

我最初在设置WPF应用程序控件的颜色时遇到了麻烦。它似乎不包含System.Windows。媒体默认包括Windows.UI.Xaml。媒体,它有一些预先填充的颜色。

I ended up using the following line of code to get it to work:

最后我用了下面一行代码让它工作:

grid.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.CadetBlue);

You should be able to change grid.Background to most other controls and then change CadetBlue to any of the other colors it provides.

您应该能够更改网格。背景到大多数其他控件,然后将CadetBlue更改为它提供的任何其他颜色。

#5


1  

// WPF

// Defined Color
button1.Background = Brushes.Green;

// Color from RGB
button2.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));

#6


0  

I doubt if any of those should work. Try: First import the namespace in the beginning of the code page as below.

我怀疑这其中的任何一个是否有效。Try:首先在代码页的开头导入名称空间,如下所示。

using System.Drawing;

then in the code.

然后在代码中。

Button4.BackColor = Color.LawnGreen;

Hope it helps.

希望它可以帮助。