17

I have an assembly that contains several user controls. For this user controls assembly I want to have a resource dictionary. All the user controls within the assembly should be able to access the resource dictionary. Do I have to add

<UserControl.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      ...
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</UserControl.Resources>

to every user control that should utilize the resource dictionary, or is there some way of putting it in one place and just referencing it?

can I then also reference it in my main application, or does that require a MergedDictionaries call as well?

Edit: the main application is in a separate project/assembly than the user controls.

Dan Vogel
  • 3,858
  • 7
  • 41
  • 57

3 Answers3

11

is there some way of putting it in one place and just referencing it?

Put your merged dictionaries reference to your resource dictionaries into your 'App.xaml' file and it will be accessible throughout your application, you will need no futher reference.

can I then also reference it in my main application, or does that require a MergedDictionaries call as well?

No the scope of 'App.xaml' falls over the entire application, so this should work fine (does for me :) ).

Update: How to reference resource dictionary stored items from user control.

Within your main project add a reference to your user control library. Your user controls will be accessible and you can use them in you application as desired.

The process of adding the resource dictionary reference in the App.xaml will mean that all controls can reference styles and data templates etc. defined in the resource dictionaries, so it it merely a matter of referencing them:

e.g.

Style="{StaticResource MyButtonStyle}"

This method works for both composite applications and regular WPF applications. Note that Visual Studio is no good at loading these styles from Linked XAML files (resource dictionary) but expression blend deals with it and will give the editor preview.

holsee
  • 1,974
  • 2
  • 27
  • 43
  • 2
    Then how can I utilize these resources in my user controls, when the user controls are in a separate project/assembly? – Dan Vogel Apr 01 '09 at 16:34
  • I have it working! Thanks. I had the resource stuff working in my App just wasn't sure how to get these resources to work in the user controls. I didn't realize it would just work when the control was added to the App. I think the fact the visual studio designer wasn't displaying it threw me. – Dan Vogel Apr 03 '09 at 23:48
  • Use Expression Blend, far better for the visual design aspect (and resources will flow down). – holsee Apr 04 '09 at 09:25
  • 3
    So I thought I had it working, and it seemed to be ok for a few days. But now I can't view my user controls in the designer. I get the error that the "StaticResource reference 'the resource name' was not found". Plus I now can't view any window that has one of those user controls on it. – Dan Vogel Apr 06 '09 at 21:37
  • Does anyone know if Visual Studion 2010 supports this now? Thanks! – kimsk Jun 17 '10 at 22:03
  • 1
    My situation is the same as dan's. For several days I had no problem with having staticresource references to the merged resourcedictionary in app.xaml, but now the staticresource references do not work in the designer. I have no idea how to fix this and have spent several hours reading about the issue now – James Joshua Street Aug 09 '13 at 01:02
  • This does not appear to work for me. The "global" resources seem to be unknown within control templates. A `XamlParseException` is thrown when using them after including the common resources file in App.xaml, indicating the resource names cannot be found within the templates. – O. R. Mapper Dec 28 '13 at 15:17
3

You can use a Pack URL to reference resource dictionaries across assemblies. For example, in Prism projects (which have multiple modules, each in its own assembly), I store my common resource dictionaries in a ResourceDictionaries folder in a project titled Common. Then I merge these dictionaries as needed into the app's modules by using markup similar to this in each module's XAML view:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Common;component/ResourceDictionaries/DemoDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

You can find further information here.

David Veeneman
  • 18,912
  • 32
  • 122
  • 187
2

If the "App.xaml" approach doesn't work for you then you might be interessted in this discussion:

Best Practices: How to handle shared resources in a modular application?

jbe

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81