Search code examples
c#wpfuser-controlsresourcedictionary

Styles from resource dictionary not found in user control


I have a resource dictionary with a bunch of styles that I am linking too in my user controls like so:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Theme/ThemedResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

This all works fine during runtime.

However I get a recurring problem in that one of the styles in the resource dictionary 'cannot be found' in whichever user control is the most recent, until the solution is rebuilt. This error will then appear again after I stop the debugging process, and again will disappear with a rebuild.

I don't think this is linked to a specific style, as when I add new styles the style that can't be found seems to change.

Any ideas how I can stop this from happening?


Solution

  • You have used a Relative URI, so it will looks for your resource file in a somewhere that you used your UserControl. It can't find you resource file because your resource file is not in the AbsolutePath.

    AbsolutePath = CurrentPath (r.g Where you used your UserControl) + RelativePath

    so Use an AbsolutePath:

       <ResourceDictionary Source="pack://application:,,,/{YourAssemblyName};component/Theme/ThemedResources.xaml" />