Wpf – Setting Window background color using resource

backgroundcolorsresourceswindowwpf

I need to use a resource to set the color of the main window in my WPF application. Since the resource declaration comes after the window declaration (I am importing a resource dictionary), I can't use a Background property in the Window object. So, I thought I would set the background this way:

<Window.Resources>
...
</Window.Resources>

<Window.Background>
    <SolidColorBrush Color="{StaticResource WindowBackgroundBrush}"  />
</Window.Background>

My syntax is a bit off, since the object won't take a brush resource for its Color property. What's the fix? Thanks for your help.

Best Answer

Try this

<Window.Background>
    <StaticResource ResourceKey="WindowBackgroundBrush" />
</Window.Background>