Webfarmer
Webfarmer

Reputation: 263

Set implicit style in code behind

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?

Upvotes: 4

Views: 3456

Answers (2)

Webfarmer
Webfarmer

Reputation: 263

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)

Upvotes: 5

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

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.

Upvotes: 0

Related Questions