tags:

views:

2604

answers:

3

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.

+6  A: 

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.

StevenH
Then how can I utilize these resources in my user controls, when the user controls are in a separate project/assembly?
Dan Vogel
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
Use Expression Blend, far better for the visual design aspect (and resources will flow down).
StevenH
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
Does anyone know if Visual Studion 2010 supports this now? Thanks!
kimsk
+2  A: 

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

A: 

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