Search code examples
wpfuser-controlsstylesresourcedictionary

Resource Dictionary Styles not available to UserControl


So I have made a resource dictionary of styles to use and included it in my UserControl:

<UserControl.Resources>
    <ResourceDictionary Source="../affinityStyles.xaml" />
</UserControl.Resources>

Which makes it available to all the controls in the UserControl, but not the UserControl itself. I am guessing this is because that bit of code comes after the UserControl tag.

How can I use Resource Dictionary defined styles for my UserControl background?


Solution

  • One option is to use DynamicResource rather than StaticResource; this postpones resolution until runtime.

    Alternately, you can use the following XAML property syntax and place it after the ResourceDictionary is merged in:

    <UserControl.Background>
        <StaticResource ResourceKey="SomeResourceKey"/>
    </UserControl.Background>