1
votes

I have a simple resource dictionary with four main files

  • MainWindow (With some controls)
  • Application
  • User Control
  • Resource Dictionary (With some styles)

I want the styles in the resource dictionary to be accessible across the application so I can use them in MainWindow and my UserControl.

I have placed a resource dictionary with a source in my Application.Resources

<ResourceDictionary Source="Assets/ButtonStyle"/>

However when I am in MainWindow adding the styles to the controls it says it cannot find the styles. It also says it needs a x:key on the ResourceDictionary tag in Application.Resources.

I have imported the styles into the Application.Resources and they work fine. However I would like to keep them in the seperate resource dictionary file as I want to distribute it and keep it in order.

Any help is greatly appricated! :)

1

1 Answers

3
votes

Add your ResourceDictionary to the MergedDictionaries property of the global App.xaml ResourceDictionary and give any other resource an x:Key:

    <Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/ButtonStyle"/>
            </ResourceDictionary.MergedDictionaries>
            <SolidColorBrush x:Key="myBrush1" Color="Blue" />
            <SolidColorBrush x:Key="myBrush2" Color="Red" />
        </ResourceDictionary>
    </Application.Resources>
</Application>