4
votes

We've an application in which we have the default control styles defined as implicit style.

XAML:

<Style TargetType="Button">
    [...]
</Style>

These styles are now applied to every button in the application.

Sometimes we change the style in code-behind to something different.

XAML:

<Style x:Key="HighlightStyle" TargetType="Button">
    [...]
</Style>

Code:

cmdButton.Style = App.Current.Resources("HighlightStyle")

Then again we want to remove the style and return to the implicit style, but this doesn't seem to be possible:

Code:

cmdButton.Style = Nothing

Results in an unstyled Button.

I've also read here http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx that all implicit style should be accessible by the TargetType-Value as Key, but this doesn't seem to work either.

Does anyone know a way around this?

2

2 Answers

5
votes

As usual, as soon as i've posted the question, i've come up with a solution:

Using the ClearValue Method on the Object clears the style property leaving it on the default style.

cmdButton.ClearValue(FrameworkElement.StyleProperty)
0
votes

What about - to add Key for default Style and instead of cmdButton.Style = Nothing; do cmdButton.Style = App.Current.Resources("DefaultButtonStyle");.

If you does not like this solution you can read a couple articles about VisualStateManager. But for implementing it you should rewrite a bit your logic.