How to reference global resources in user controls in wpf class library projects

HoWe Yu 41 Reputation points
2024-06-21T05:17:59.38+00:00

I created a WPF class library, and then added a UserControl "Card.xaml", and a resource dictionary "GlobalRes.xaml", I hope to use the style resources in GlobalRes in the Card control. As shown below:

GlobalRes.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Style x:Key="CloseButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Border"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</ResourceDictionary>

Card.xaml

<UserControl x:Class="WpfLib.Card"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfLib"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Width="30" Height="30" HorizontalAlignment="Right" VerticalAlignment="Top"
                Style="{StaticResource CloseButton}"/>
    </Grid>
</UserControl>

I know I can add resource references in Card.xaml:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="GlobalRes.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

but I want to be able to add resources like in App.xml and then all controls can reference them.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,704 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,551 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 780 Reputation points Microsoft Vendor
    2024-06-21T06:28:41.0333333+00:00

    Hi,@HoWe Yu. Welcome to Microsoft Q&A. 

    You could try the following method to reference global resources in user controls

     

    In App.xaml, make the following adjustments to <Application.Resources>

    
        <Application.Resources>
    
            <ResourceDictionary>
    
                <ResourceDictionary.MergedDictionaries>
    
                    <ResourceDictionary Source="GlobalRes.xaml"></ResourceDictionary>
    
                </ResourceDictionary.MergedDictionaries>
    
            </ResourceDictionary>
    
        </Application.Resources>
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments