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?
source share