DaemonFire
DaemonFire

Reputation: 863

How to apply styles in WPF without overriding packages?

I have the package Material Design Themes installed which changes the appearance of buttons. I would like to use styles with my buttons but when a style is used the button reverts to the default appearance with the style applied. Example

How would I apply a style without overriding the package?

MainWindow

  <Window.Resources>
        <Style TargetType="Button" x:Key="redFont">
            <Setter Property="Foreground" Value="red"/>
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Without style"/>
        <Button Style="{StaticResource redFont}" Content="With style"/>
    </StackPanel>

App.xaml

<Application x:Class="WPF_TestMaterialSetterProperties.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
    </Application.Resources>
</Application>

Upvotes: 1

Views: 230

Answers (1)

cunningdave
cunningdave

Reputation: 1480

The style is overriding your derived style. You need to make your style "BasedOn" the generic type style to inherit the style you've imported.

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" x:Key="redFont">
   ...

That package is overriding the default theme that your app is using. Your new style is "based-on" that override.

Upvotes: 3

Related Questions