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

HoWe Yu 86 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.

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.