KMC
KMC

Reputation: 20046

WPF how to restore Button background color

This question felt so simple but I just can't find the answer:

How to change a Button back to its default? My VS2010 start giving me button with strange color and I have to manually set the Button to look like its default self.

I tried:

btn.Background = null; // only make it transparent, not default background

Anyone?

enter image description here

Upvotes: 9

Views: 12248

Answers (1)

HCL
HCL

Reputation: 36775

Use the ClearValue-method to restore the default.

btn.ClearValue(Button.BackgroundProperty);

or

btn.ClearValue(Control.BackgroundProperty);

This sets back the background-property of the button. But if you have changed the buttons template, this will not help. In this case, look for explicit declarations of the Button.Template-property or for a style that sets the Button.Template-property. Look especially in your App.xaml if there is something like

<style TargetType="Button">
  ...
</style>

or

<style TargetType="{x:Type Button}">
  ...
</style>

Upvotes: 24

Related Questions