Skip to main content

Microsoft Silverlight

Answered Question Change Button Background colorRSS Feed

(1)

Kostja
Kostja

Member

Member

38 points

16 Posts

Change Button Background color

Hello

Please help me

How can i Change Button background color in runtime?

texmex5
texmex5

Contributor

Contributor

2239 points

382 Posts

Answered Question

Re: Change Button Background color

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

  myButton.Background = new SolidColorBrush(new Color(255,255,255));
 

anandav200
anandav200

Member

Member

53 points

56 Posts

Re: Change Button Background color [[ Setting from string ]]

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

Regards,
Anand

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7202 points

1,342 Posts

Re: Change Button Background color [[ Setting from string ]]

Hi,

Look here.

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

Thomas D
Thomas D

Member

Member

46 points

66 Posts

Re: Change Button Background color [[ Setting from string ]]

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);
  This must be a very easy thing, however, up to now I just can't figure it out, what's wrong ...

EDIT: I've just changed the color to Green and experienced that it only gets a slight green touch in reflecting. Is it possible that this property just changes the reflection and not the overall color style of a standard button? However, disabling the Button's with tooltipClose.Style = null; didn't helped either.

Sharobel
Sharobel

Member

Member

2 points

6 Posts

Re: Change Button Background color [[ Setting from string ]]

Hi,

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,

Sharobel

 

headbiznatch
headbizn...

Member

Member

6 points

3 Posts

Re: Change Button Background color [[ Setting from string ]]

 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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities