Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared resource dictionary between several user controls and across assemblies

Tags:

c#

resources

wpf

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.

like image 628
Dan Vogel Avatar asked Mar 31 '09 20:03

Dan Vogel


People also ask

What is a resource dictionary in WPF?

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology.

How do you create a resource dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu.

What is merged dictionary in WPF?

This feature provides a way to define the resources portion of a WPF application outside of the compiled XAML application. Resources can then be shared across applications and are also more conveniently isolated for localization.


2 Answers

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.

like image 152
2 revs Avatar answered Oct 18 '22 09:10

2 revs


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.

like image 39
David Veeneman Avatar answered Oct 18 '22 08:10

David Veeneman