Home > Blockchain >  Wpf ResourceDictionary for Style and Brushes and how to MergedDictionaries
Wpf ResourceDictionary for Style and Brushes and how to MergedDictionaries

Time:10-15

I hope someone can clarify the situation I have below.

I have a simple CustomControl style based on a Button in a ResourceDictionary named 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>

I also have a ResourceDictionary named Brushes as below:

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

I also have a Themes folder with Generic.xaml which has the MergedDictionaries as below:

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

I have tried merging the ResourceDictionary for the Brushes within the Generic.xaml as I understood it is best to merge all ResourceDictionary's within Generic.xaml?

But the only way I can get this to work is with an additional MergedDictionaries for the Brushes within the SHButtonStyle.xaml. Is this correct or what am I missing when merging the ResourceDictionary's within the Generic.xaml.

Thank you in advance for your assistance

CodePudding user response:

Either merge the brushes resource dictionary in theme/generic.xaml directly, or merge them both at global level in 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>

The MergedDictionaries of themes/generic.xaml usually contains "standalone" resource dictionaries for custom controls without any dependencies to resources in other resource dictionaries.

  • Related