Set implicit style in code

We have an application in which we have default management styles defined as an implicit style.

XAML:

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

These styles are now applied to each button of the application.

Sometimes we change the style in the code to another.

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 is not possible:

code:

 cmdButton.Style = Nothing 

Results in unchanged Button .

I also read here http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx that all implicit style should be accessible using TargetType-Value as a key, but that doesnโ€™t mean t. seems to work too.

Does anyone know about this?

+4
source share
2 answers

As usual, as soon as I posted the question, I came up with a solution:

Using the ClearValue method on an object clears the style property, leaving it by default.

 cmdButton.ClearValue(FrameworkElement.StyleProperty) 
+5
source

How about - add Key by default Style and instead of cmdButton.Style = Nothing; make cmdButton.Style = App.Current.Resources("DefaultButtonStyle"); .

If you do not like this solution, you can read a couple of articles about VisualStateManager . But to implement it, you have to rewrite your logic a bit.

0
source

Source: https://habr.com/ru/post/1386573/


All Articles