WPF and UserControls default styles - best practices?

I have the following situation (simplified):

In my ResourceDictionary, I defined a couple of named styles for the label:

<Style x:Key="sNormalLabel" TargetType="Label" > <Setter Property="FontFamily" Value="Verdana" /> <Setter Property="FontSize" Value="12" /> <!-- ... --> </Style> <Style x:Key="sHeaderLabel" TargetType="Label" BasedOn="{StaticResource sNormalLabel}" > <Setter Property="FontSize" Value="16" /> </Style> 

And then I do one of them by default, referencing it using BasedOn, in a style that doesn't have a key:

 <!-- Apply Normal label automatically --> <Style TargetType="Label" BasedOn="{StaticResource sNormalLabel}" /> 

I thought it’s very convenient to apply the usual label style automatically, so I know that if in the future our design team decides to go with Wingdings or something else, it’s very easy to change it for the whole application (in fact, all applications that have the same ResourceDictionary).

Creating a UserControl I want to add a shortcut at the top with the sHeaderLabel style, as applicable, I apply

 Style={StaticResource sHeaderLabel} 

in USerControl XAML and everything looks great in the designer.

However, when I add UserControl to MainWindow, the title label returns to the sNormalLabel style (automatically applies).

I assume that it relates to the order in which the styles are applied, the UserControl is styled by MainWindow after its creation, and the sHeaderLabel style is overwritten automatically.

This leaves me with a couple of unresolved questions:

  • This is unusual and contrary to best practice for using automatically applied styles, how did I do it? I thought it was very practical, but maybe this is the wrong way.
  • Is there any way to exclude certain controls from applying these automatic styles? The only way I can think of is to extend the Label with HeaderLabel and apply a different resource key so that HeaderLabel automatically applies sHeaderLabel style to it. Is there another way?

If anyone has hands-on experience with something like this, I would appreciate some pointers.

EDIT: Interestingly, I just realized that I only see these problems with the Font * properties. Setting the foreground / background brushes differently in the standard / top style leaves a mark in the UserControl (which has the sHeaderLabel style) with the color CORRECT . Installing FontWeight in bold in sHeaderLabel has no effect.

I also created TextBox styles in the same way with the HeaderTExtBox style, and I also do not see a problem there, only with shortcuts.

Hooray!

./Fredrick

+4
source share
2 answers

It may be good practice to avoid applying basic automatic styles to standard types, as the results can be unpredictable.

In response to what ultimately became your problem, a good example is applying style to a TextBlock . This style will apply to the Label , Button and any other control anywhere in the framework that may contain a TextBlock that does not override the style that you set.

You cannot β€œundo” a style when applied to all instances of a type; you can only override a style with the new style. If you cannot accurately predict everything in your application that this will affect (for example, the control style you made), you should avoid using the automatic style in favor of the x:Key setting.

+1
source

Well, I figured it out.

I also had the default style for TextBlock , and the removal was a global fix. According to MSDN, Label and TextBlock have no inheritance relationship, and TextBlock cannot be obtained from Control , which is interesting.

Maybe Label uses TextBlock internally for rendering, I don’t know this, but obviously the TextBlock style influenced Label behavior by default.

0
source

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


All Articles