Astralogic
Astralogic

Reputation: 43

A style I defined in a resource dictionary won't resolve

So I added a resource dictionary to a resources folder in my project called Styles.xaml like so:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApp1.Resources">

<Style x:Key="groupBoxHeader"
       TargetType="{x:Type Border}">
    <Setter Property="CornerRadius"
            Value="4" />
    <Setter Property="Padding"
            Value="5 1 5 1" />
    <Setter Property="TextBlock.Foreground"
            Value="White" />
    <Setter Property="TextBlock.FontSize"
            Value="14" />
    <Setter Property="TextBlock.FontWeight"
            Value="Bold" />
</Style>

But when I try to bind it to a style property of a border like so:

Style="{StaticResource groupBoxHeader}"

it says "the resource 'groupBoxHeader' could not be resolved". Why is this happening?

Upvotes: 1

Views: 518

Answers (1)

jjw
jjw

Reputation: 294

First, you have to add the code to merge the resource in the App.xaml file as below. (I assume the name of your resource file is StyleResource.xaml and it is in the Resources folder.)

// In the App.xaml

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

If you hate this way then replace StaticResource keyword to the DynamicResource keyword.

Upvotes: 1

Related Questions