Application Style Inheritance (WPF)

I am trying to inherit application-level styles for a specific Window in my WPF application, but I am unable to get it to inherit, and not just override existing styles.

In App.xaml (under App.Resources element ), I define the style as such:

 <Style TargetType="Button"> <Setter Property="Padding" Value="6"/> <Setter Property="FontWeight" Value="Bold"/> </Style> 

And in XAML for a specific Window , I define the following in Window.Resources :

 <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="Padding" Value="6"/> <Setter Property="FontWeight" Value="Bold"/> </Style> 

The problem is that the old (app) style is ignored, as if the last (window) style has overridden it. The BasedOn attribute BasedOn set, which is intended to indicate that existing styles should be inherited, as far as I know. Removing an attribute also does not help. The only potential reason I can think of is that {StaticResource {x:Type Button}} is only the default WPF standard, not the one I defined in App.xaml.

I know that this style behavior could be accomplished using the x:Key attribute, but I was hoping for a more elegant way that allows me to apply inherited styles to all controls within an area (like application / window).

Update

Thank you for your responses. You are really right that in a typical application everything works as expected. The difference is that I did not accidentally mention that the style in App.xaml contained inside a ResourceDictionary , as such:

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="SettingsDictionary.xaml"/> <ResourceDictionary> <Style x:Key="DefaultButton" TargetType="Button"> <Setter Property="Padding" Value="4"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

Any suggestion on how to fix the situation in this case?

+4
source share
2 answers

EDIT

After some research, I found that x: Key is automatically generated if TargetType is given. So the style in App.xaml is correct. However, the wpf designer lacks resource management skills and does not display both styles. If you create and run a project, both styles will be applied.

If your computer and VS2008 behave like the one on which I checked your code.

Hope this helps.

EDIT 2

Resources and collaborative dictionaries in App.xaml have always been fancy. I solved the problem by moving the first style declaration from the combined dictionary, for example:

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!--<ResourceDictionary Source="SettingsDictionary.xaml"/>--> </ResourceDictionary.MergedDictionaries> <Style TargetType="Button"> <Setter Property="Foreground" Value="Red"/> <Setter Property="Padding" Value="4"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style> </ResourceDictionary> </Application.Resources> 

Please also note that giving the style an explicitly set key other than {x:Type Button} will make it non-standard and will not be automatically applied.

It is generally recommended that you specify concatenated dictionaries only for resources from another file and encoded resources in the default space, as described above.

+5
source

I would repeat Jeff Wins' comment, surprised that your approach does not work as we would like. In fact, I cannot reproduce your problem with the following steps:

  • Created a new project using the WPF VS 2008 Application Wizard.
    • which will result in App.xaml and Window1.xaml, like your example
  • Added a standard button from the toolbar in Window1.
  • Your fragments are inserted as is, but modified one property to observe the desired effect in the first place (with identical properties / values โ€‹โ€‹that are not what you intended to demonstrate, I think).

Well, this just works fine, i.e. a button in Window1 inherits the properties of both styles and changes the properties in that one of them correctly affects the button. Therefore, should there be something strange happening off-screen in your project / environment? Have you tried a simple example for playback?

+1
source

All Articles