How to use style from resource dictionary in Custom Control Library project

Sarah 186 Reputation points
2022-09-29T12:12:59.67+00:00

I have created a project of type "WPF Custom Control Library" (.Net Core 3.1) and a resource dictionary named "GResourceDic" with the following content:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
  
    <Style x:Key="BorderStyle" TargetType="{x:Type Border}">  
        <Setter Property="BorderThickness" Value="1"/>  
        <Setter Property="BorderBrush" Value="AliceBlue"/>  
        <Setter Property="VerticalAlignment" Value="Stretch"/>  
        <Setter Property="Background" Value="Beige"/>  
    </Style>  
  
</ResourceDictionary>  

In Generic.xaml I have added the following:

<ResourceDictionary  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:Common">  
    <Style TargetType="{x:Type local:CustomControl1}">  
        <Setter Property="Template">  
            <Setter.Value>  
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">  
                    <Border Background="{TemplateBinding Background}"  
                            BorderBrush="{TemplateBinding BorderBrush}"  
                            BorderThickness="{TemplateBinding BorderThickness}">  
                    </Border>  
                </ControlTemplate>  
            </Setter.Value>  
        </Setter>  
    </Style>  
  
    <ResourceDictionary.MergedDictionaries>  
        <ResourceDictionary Source="ResourceDics/GResourceDic.xaml"/>  
    </ResourceDictionary.MergedDictionaries>  
  
</ResourceDictionary>  

I then created a Window:

<UserControl x:Class="Common.BarView"  
             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"   
             mc:Ignorable="d"   
             d:DesignHeight="50"  
             d:DesignWidth="700">  
      
      
    <Grid>  
        <Border Style="{StaticResource BorderStyle}"/>  
    </Grid>  
</UserControl>  

<Window x:Class="Common.ActView"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:Common"  
        mc:Ignorable="d"  
        WindowStartupLocation="CenterScreen"   
        Background="White"  
        WindowStyle="None"  
        Height="210"  
        Width="450" >  
  
  
    <Grid>  
        <Grid.RowDefinitions >  
            <RowDefinition Height="30"/>  
            <RowDefinition Height="*"/>  
        </Grid.RowDefinitions>  
  
        <local:MenuBarView Grid.Row="0"/>  
  
        <Grid Grid.Row="1"  
              Margin="20">  
  
            <Border Style="{StaticResource BorderStyle}"/>  
        </Grid>  
  
    </Grid>  
</Window>  

I always get the following Felhler:

Error XDG0062 The resource "BorderStyle" could not be resolved.

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,685 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
768 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,866 Reputation points Microsoft Vendor
    2022-09-30T06:21:06.007+00:00

    On the one hand:

    WPF is unable to resolve the resource dictionary that you are trying to merge. When you create a merged dictionary, WPF follows a set of rules to attempt to resolve the URI. In your case, the URI is ambiguous, so WPF is unable to reliably resolve it.

    Change the Source URI in the ResourceDictionary to an absolute pack URI.

    For example, if your project is called Common, then you could change the Source in GResourceDic.xaml to:

       <ResourceDictionary Source="Common;component/ResourceDics/GResourceDic.xaml" />  
    

    See this page on MSDN for further details on Pack URIs in WPF.

    On the other hand:
    ResourceDictionary needs to be in Resources. You can delete the ResourceDictionary in Generic.xaml. Add the following code in UserControl(BarView:UserControl.Resources ) and Window(ActView: Window.Resources ) respectively.

    < Window.Resources>   
    <ResourceDictionary>  
      <ResourceDictionary.MergedDictionaries>  
        <ResourceDictionary Source="Commen;component/ResourceDics/GResourceDic.xaml" />  
      </ResourceDictionary.MergedDictionaries>  
    </ResourceDictionary>  
    < / Window.Resources>  
    

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful