如何避免在 WPF 中覆盖 window-wide 样式?

How can I avoid overriding a window-wide style in WPF?

我正在为我的 WPF 应用程序使用 MaterialDesign nuget 包。 根据 tutorial,通过应用 window-wide 属性,每个元素都将继承 MaterialDesign 样式。 但是,如果我将自定义样式应用于元素,该元素将失去其 MaterialDesign 样式。 我可以通过应用内联样式来解决这个问题,但这是非常重复且容易出错的。

我觉得图片更好看:

这是我的 xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestApp"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" x:Class="TestApp.MainWindow"
        mc:Ignorable="d"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="13"
        TextOptions.TextFormattingMode="Ideal" 
        TextOptions.TextRenderingMode="Auto"        
        Background="{DynamicResource MaterialDesignPaper}"
        FontFamily="{DynamicResource MaterialDesignFont}"
        Title="MainWindow" Height="450" Width="600">

        <!--All of the above is meant to apply Material Design to the entire Window-->

    <StackPanel>
        <StackPanel.Resources>

            <!--This style overrides the Material Design style entirely,
            instead of just Margin and Horizontal Alignment--> 
            <Style x:Key="SpacedButton" TargetType="Button">
                <Setter Property="Margin" Value="0 10"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
            </Style>
        </StackPanel.Resources>

        <!--Material Design only works by applying properties directly to elements-->
        <Button Content="Button #1" HorizontalAlignment="Center" Margin="0 10"/>
        <Button Content="Button #2" Style="{DynamicResource SpacedButton}"/>
        <Button Content="Button #3" Style="{DynamicResource SpacedButton}"/>
        <Button Content="Button #4" HorizontalAlignment="Center" Margin="0 10"/>
    </StackPanel>
</Window>

如您所见,只有具有内联属性的元素保留了 MaterialDesign 样式,但是通过应用自定义样式,MaterialDesign 样式丢失了。

如何确保 MaterialDesign 应用于每个元素,同时仍然能够使用自定义样式覆盖特定属性?

抱歉,如果某些术语有误,我是 WPF 新手。

使用 Style.BasedOn 从范围内的任何其他样式继承属性:

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

或特定的:

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