user8162574
user8162574

Reputation:

WPF Resource Dictionary In Application Resources

I have a simple resource dictionary with four main files

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.

Upvotes: 0

Views: 5461

Answers (1)

mm8
mm8

Reputation: 169420

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>

Upvotes: 2

Related Questions