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:
<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