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.
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.
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.
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