Search code examples
xaml

How to reference a dictionary in another project WPF


[Custom Control Wpf Library]

Test
-->Resources
    -->Resources
       -->Dictionary1.xaml (Reusable across multiple project)
       -->Dictionary2.xaml(Reusable across multiple project)

[WPF main UI] TestRD

-->Resources
    -->Resources
       -->Dictionary9.xaml(Specific to the project)

XAML: Dictionary1.xml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="brush" Color="Red"/>
</ResourceDictionary>

Dictionary2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="brush" Color="Green"/>
</ResourceDictionary>

Dictionary9.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--How can I use the resource from another DLL here-->
</ResourceDictionary>
  1. How can I reference Dictionnary1 or dictionary2 in Dictionary9?

  2. Is it advisable, especially in a modular project?

I have some styling I would like to move globally so I can reuse and stop copy and pasting them.


Solution

    1. How can I reference Dictionnary1 or dictionary2 in Dictionary9?

    Try this:

    Dictionary9.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <!--How can I use the resource from another DLL here-->
        <ResourceDictionary Source="pack://application:,,,/Resources;component/Resources/Dictionary1.xaml" />
        <ResourceDictionary Source="pack://application:,,,/Resources;component/Resources/Dictionary2.xaml" />
    </ResourceDictionary>
    

    Note that the 1st "Resources" is your lib name and the 2nd "Resources" is the folder name inside your lib. You can find more info here.

    1. Is it advisable, especially in a modular project?

    IMO, is totally OK if you're reusing or need to separate these dictionaries.