Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine imported and local resources in WPF user control

I'm writing several WPF user controls that need both shared and individual resources.

I have figured out the syntax for loading resources from a separate resource file:

<UserControl.Resources>     <ResourceDictionary Source="ViewResources.xaml" /> </UserControl.Resources> 

However, when I do this, I cannot also add resources locally, like:

<UserControl.Resources>     <ResourceDictionary Source="ViewResources.xaml" />     <!-- Doesn't work: -->     <ControlTemplate x:Key="validationTemplate">         ...     </ControlTemplate>     <style x:key="textBoxWithError" TargetType="{x:Type TextBox}">         ...     </style>     ... </UserControl.Resources> 

I've had a look at ResourceDictionary.MergedDictionaries, but that only lets me merge more than one external dictionary, not define further resources locally.

I must be missing something trivial?

It should be mentioned: I'm hosting my user controls in a WinForms project, so putting shared resources in App.xaml is not really an option.

like image 473
Tor Haugen Avatar asked Aug 26 '09 10:08

Tor Haugen


People also ask

How to add ResourceDictionary in XAML?

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. Here, you define a resource dictionary in a separate XAML file called Dictionary1.

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

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.

What is UserControl WPF?

User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.


2 Answers

I figured it out. The solution involves MergedDictionaries, but the specifics must be just right, like this:

<UserControl.Resources>     <ResourceDictionary>         <ResourceDictionary.MergedDictionaries>             <ResourceDictionary Source="ViewResources.xaml" />         </ResourceDictionary.MergedDictionaries>         <!-- This works: -->         <ControlTemplate x:Key="validationTemplate">             ...         </ControlTemplate>         <style x:key="textBoxWithError" TargetType="{x:Type TextBox}">             ...         </style>         ...     </ResourceDictionary> </UserControl.Resources> 

That is, the local resources must be nested within the ResourceDictionary tag. So the example here is incorrect.

like image 125
Tor Haugen Avatar answered Sep 22 '22 00:09

Tor Haugen


You can define local resources inside MergedDictionaries section:

<UserControl.Resources>     <ResourceDictionary>         <ResourceDictionary.MergedDictionaries>             <!-- import resources from external files -->             <ResourceDictionary Source="ViewResources.xaml" />              <ResourceDictionary>                 <!-- put local resources here -->                 <Style x:key="textBoxWithError" TargetType="{x:Type TextBox}">                     ...                 </Style>                 ...             </ResourceDictionary>         </ResourceDictionary.MergedDictionaries>     </ResourceDictionary> </UserControl.Resources> 
like image 35
Lu55 Avatar answered Sep 20 '22 00:09

Lu55