Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Change Button Background color
6 replies. Latest Post by headbiznatch on May 4, 2009.
(1)
Kostja
Member
38 points
16 Posts
06-11-2008 4:57 AM |
Hello
Please help me
How can i Change Button background color in runtime?
texmex5
Contributor
2239 points
382 Posts
06-11-2008 5:03 AM |
myButton.Background = new SolidColorBrush(Colors.Red);
myButton.Background = new SolidColorBrush(new Color(255,255,255));
anandav200
53 points
56 Posts
07-09-2008 2:14 AM |
Thanks..But how will i take the color from String (Red, Blue , Yellow). My color settings are stored in database and are fected as string on runtime
Sergey.L...
7202 points
1,342 Posts
07-09-2008 2:35 AM |
Hi,
Look here.
Thomas D
46 points
66 Posts
03-20-2009 7:31 AM |
Changing a Button's color only works for Foreground for me. When using the following code, Foreground is changed to white, but Background still remains as usual.
Button tooltipClose = new Button();tooltipClose.Content = "x";tooltipClose.Background = new SolidColorBrush (Colors.Black);tooltipClose.Foreground = new SolidColorBrush (Colors.White);
Sharobel
2 points
6 Posts
04-17-2009 8:50 AM |
I am facing the same problem (Changing a Button's color only works for Foreground, not for Background).
private void Button_Clicked(object sender, RoutedEventArgs e)
{
(sender as Button).Background = new SolidColorBrush(Colors.Red);
(sender as Button).Foreground = new SolidColorBrush(Colors.Blue) ;
}
Could anyone please reply if u had already resolved this issue?
Thanks,
headbizn...
6 points
3 Posts
05-04-2009 6:27 PM |
Hi all,
I too had this issue - trying to apply styles or change background colors always resulted in the default white/silver button with a slight hue alteration depending on the brush I tried to apply. I "solved" it by making a full copy of the button control template instead of just creating a brush resource. This is easy to do in Blend. Now you can apply this style to your button and it will behave as you expect. Much more markup when all you wanted to do was change the button's color, but it works.
For what it is worth, I did not have this issue in WPF - I would create the control in code, select a background brush resource and apply it before dropping the control on my grid.
I am educating myself about OnApplyTemplate and GetTemplateChild - seems like it should be possible to not have to copy the template and instead just change the Rectangle.Fill for the "BackgroundGradient" key defined in the default template. Right?
Anyway, hope this helps you solve your problem for now. Best wishes.