How to override a global style (that doesn't have an x:Key), or alternatively apply a named style to all type-targeted controls? How to override a global style (that doesn't have an x:Key), or alternatively apply a named style to all type-targeted controls? wpf wpf

How to override a global style (that doesn't have an x:Key), or alternatively apply a named style to all type-targeted controls?


Try this:

<Style TargetType="{x:Type StackPanel}" BasedOn="{StaticResource {x:Type StackPanel}}">  <!-- ... --></Style>

I have declared my base styles in a ResourceDictionary in App.xaml, if i override them in a specific window like this, it usually works.


Somewhere in global resource dictionary you define a base style with a key. This base style is targeted to a type which is base for all types for which you intend to apply that style. Then you define styles which target the types that you want and are based on the above mentioned base style.

<Style    x:Key="upDownBaseStyle"    TargetType="{x:Type Control}">    <Setter      Property="Margin"      Value="2" />    <Setter      Property="HorizontalAlignment"      Value="Stretch" />    <Setter      Property="VerticalAlignment"      Value="Center" />  </Style>  <Style    TargetType="{x:Type xceed:IntegerUpDown}"    BasedOn="{StaticResource upDownBaseStyle}">  </Style>  <Style    TargetType="{x:Type xceed:DoubleUpDown}"    BasedOn="{StaticResource upDownBaseStyle}">  </Style>

Now the two last styles are applied to all the IntegerUpDown and DoubleUpDown controls within your application without any mention of a key.

So the base rule: the base style must have the key to refer to it, and the derived styles may not and therefore they can be applied without any keys - only by target types.


I would suggest that what you may be looking for here is a master style or behavior scenario that is generally achieved by creating a user control. If you were to create a new button control with your 'global' style applied to it, then anywhere you would use that control you could simply add any 'New styles or override styles if / when they were needed.

If you haven't created a user control, they are easy to implement.