如何在windows phone应用中改变按钮的背景颜色?

时间:2022-11-20 13:58:26

I am developing window phone 7 application in C# & silverlight 4. I am new to the silverlight.I have two buttons in my application for different purposes. I want to change the color of the button dynamically when the button gets clicked. So I am using the following code

我正在开发c#和silverlight 4中的windows phone 7应用程序。我是silverlight的新手。我的应用程序中有两个按钮用于不同的目的。我想在按钮被点击时动态地改变按钮的颜色。所以我用的是下面的代码

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

But it is not working. Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.

但这并不奏效。你能提供我解决上述问题的任何代码或链接吗?如果我做错了什么,请指导我。

3 个解决方案

#1


4  

Changing the styling of a button from it's own click event comes with a catch. Peter elaborates here.

从按钮自己的单击事件中更改按钮的样式会带来一个捕获。彼得在这里了。

Why can't I change the Background of my Button on a Click event? - Peter Torr's Blog

为什么我不能更改单击事件上按钮的背景?——彼得·托的博客

#2


2  

You can simply execute IncomeButton.UpdateLayout() after changing button's color.

您只需在更改按钮的颜色后执行IncomeButton.UpdateLayout()即可。

#3


1  

I also ran into that "simple" problem. Instead of using Peter's "button styling change" thing, I simply placed a rectangle below the button and changed it's color with the Rectangle.Fill property in the Click event of the button. Works fine for me.

我也遇到了这个“简单”的问题。我没有使用Peter的“按钮样式更改”,只是在按钮下面放置了一个矩形,并用矩形改变了颜色。在按钮的单击事件中填充属性。对我来说很不错。

#1


4  

Changing the styling of a button from it's own click event comes with a catch. Peter elaborates here.

从按钮自己的单击事件中更改按钮的样式会带来一个捕获。彼得在这里了。

Why can't I change the Background of my Button on a Click event? - Peter Torr's Blog

为什么我不能更改单击事件上按钮的背景?——彼得·托的博客

#2


2  

You can simply execute IncomeButton.UpdateLayout() after changing button's color.

您只需在更改按钮的颜色后执行IncomeButton.UpdateLayout()即可。

#3


1  

I also ran into that "simple" problem. Instead of using Peter's "button styling change" thing, I simply placed a rectangle below the button and changed it's color with the Rectangle.Fill property in the Click event of the button. Works fine for me.

我也遇到了这个“简单”的问题。我没有使用Peter的“按钮样式更改”,只是在按钮下面放置了一个矩形,并用矩形改变了颜色。在按钮的单击事件中填充属性。对我来说很不错。