用于样式和画笔的 Wpf ResourceDictionary 以及如何合并字典

Wpf ResourceDictionary for Style and Brushes and how to MergedDictionaries

我希望有人能澄清我下面的情况。

我有一个简单的 CustomControl 样式,它基于名为 SHButtonStyle.xaml

的 ResourceDictionary 中的按钮
<Style TargetType="{x:Type local:SHButton}">
    <Setter Property="Background" Value="{StaticResource ResourceKey= Background}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SHButton}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我还有一个名为 Brushes 的 ResourceDictionary,如下所示:

<SolidColorBrush x:Key="Background" Color="Red"/>

我还有一个带有 Generic.xaml 的主题文件夹,其中包含如下 MergedDictionaries:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/TestCustomControl;component/SHButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

我已经尝试在 Generic.xaml 中合并画笔的 ResourceDictionary,据我所知,最好在 Generic.xaml?

中合并所有 ResourceDictionary

但我能让它工作的唯一方法是在 SHButtonStyle.xaml 中为画笔添加一个额外的 MergedDictionaries。这是正确的还是在 Generic.xaml.

中合并 ResourceDictionary 时我遗漏了什么

提前感谢您的帮助

要么直接合并 theme/generic.xaml 中的画笔资源字典,要么在 App.xaml:

中在全局级别合并它们
<Application x:Class="WpfApp1.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>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/TestCustomControl;component/Brushes.xaml"/>
                <ResourceDictionary Source="/TestCustomControl;component/themes/generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

themes/generic 的 MergedDictionaries。xaml 通常包含自定义控件的“独立”资源字典,不依赖于其他资源字典中的资源。