0

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

我有一个基于名为 SHButtonStyle.xaml 的 ResourceDictionary 中的按钮的简单 CustomControl 样式

<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 的 Themes 文件夹,它的 MergedDictionaries 如下:

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

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

但是我可以让它工作的唯一方法是在 SHButtonStyle.xaml 中为画笔添加一个额外的 MergedDictionaries。这是正确的还是在 Generic.xaml 中合并 ResourceDictionary 时我遗漏了什么。

预先感谢您的帮助

4

1 回答 1

1

要么直接合并 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>

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

于 2021-10-12T18:27:00.683 回答