样式和画笔的Wpf资源字典以及如何合并字典



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

我有一个简单的CustomControl样式基于一个按钮在一个名为SHButtonStyle.xaml的资源字典

<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>

我也有一个名为画笔的资源字典,如下所示:

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

我也有一个主题文件夹与通用。xaml的MergedDictionaries如下:

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

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

但是我能让这个工作的唯一方法是在SHButtonStyle.xaml中为画笔添加一个额外的合并字典。这是正确的吗?或者我在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>

主题/通用的MergedDictionaries。Xaml通常包含"standalone"用于自定义控件的资源字典,不依赖于其他资源字典中的资源。

最新更新