Search code examples
wpfxamlresourcedictionary

Unable to reference BrushResource in linked ResourceProject from Style ResourceDictionary


I have a project in my application where I keep all of my Resources & reusable custom control classes (including styles, brushes, custom controls, etc.).

The App.xaml file of my Application's "Main" project references the ResourceLibrary file that references all my organized ResourceDictionary files like so:

     <Application.Resources>
   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ControlResources;component/ResourceDictionaries/ResourceLibrary.xaml"/>
            <ResourceDictionary Source="Simple Styles.xaml"/>     
        </ResourceDictionary.MergedDictionaries>

        <Style TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="Segoe UI" />
        </Style>

        <Style TargetType="{x:Type Rectangle}" /> 

    </ResourceDictionary>


</Application.Resources>

Note: I am following this article here that includes a big-fix (the Rectangle style included above).

Below is a snapshot of what my "ControlResources" project looks like.

This is what my "ControlResource" project outline looks like

Referencing the style and customControl resources works great when I reference them from my main project, "GlenwareMaster", but when referencing the brush resources under "Brushes" from any of the style resources, the application is clearly not finding them.

My question: Can I just add a project-self-referencing link (an inherent right-click blend feature?). How can I get the style ResourceDictionaries to locate the Brush ResourceDictionaries in the same project?

Thanks in advance!


Solution

  • How are you referencing the brush resource dictionaries in your style dictionaries? Make sure you're using pack URIs and that they are correct.

    My standard practice is to use the absolute pack uri format in any resource dictionaries that I plan to reference within another resource dictionary- leads to less errors if you end up moving it to another project in the future. When you reference brushes in your common style resource dictionaries, Try something like:

    <ResourceDictionary Source="pack://application:,,,/ControlResources;component/ResourceDictionaries/Brushes/MyBrushes.xaml"/>
    

    You can read more about Pack URIs here: http://msdn.microsoft.com/en-us/library/aa970069.aspx