1

So I created a ResourceDictionary that looks like this

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

Now my question is how do I get a hold of that key so I can use it for my background property on my MainWindow?

<Window ...
        Background="{DynamicResource PrimaryColor}">
mm8
  • 163,881
  • 10
  • 57
  • 88
JohnA
  • 564
  • 1
  • 5
  • 20

1 Answers1

3

You need to merge the ResourceDictionary into your App.xaml:

<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="YourResourceDict.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Once it's in scope, you could reference any resource using either {DynamicResource key} or {StaticResource key}

What's the difference between StaticResource and DynamicResource in WPF?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I forgot to add that into the question but I actually did merge it into my `App.xml` and I am trying to use `Background="{DynamicResource PrimaryColor}"` which didnt work nor did `Background="{StaticResource PrimaryColor}"` – JohnA Mar 05 '19 at 15:38
  • Are you getting an error when you use `StaticResource` or what happens? What's the contents of the window? – mm8 Mar 05 '19 at 15:39
  • 1
    Nevermind, I am using Visual Studio 2019 RC and It didnt display the error message I was getting in another file, the issue is resolved thanks! I'll accept the answer in 6 minutes. – JohnA Mar 05 '19 at 15:40