Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways of using ResourceDictionary.MergedDictionaries

Tags:

c#

xaml

I was going through some code in our product and saw some colleagues using ResourceDictionary.MergedDictionaries in a way I had not seen it used before:

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <toolTips:ToolTips />
                <styles:ControlStyles />
                <icons:IconDictionary />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

tooltips:ToolTips and all the other elements in the MergedDictionaries are ResourceDictionaries.

The regular way of using these according to the internet is to use <ResourceDictionary Source="uri to your xaml file"/>.

So is there any practical difference between both?

If this way works why isn't it used more often as it plays well with code completion?

like image 317
RedX Avatar asked Jan 20 '21 16:01

RedX


People also ask

What can we store in a ResourceDictionary?

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 add a ResourceDictionary?

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

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.

How do I merge a resource dictionary in WPF?

To add a merged dictionary through code, you obtain a reference to the desired primary ResourceDictionary , get its MergedDictionaries property value, and call Add on the generic Collection that's contained in MergedDictionaries . The object you add must be a new ResourceDictionary .


1 Answers

I've used ResourceDicionary this way only once on a big project and it was benefical in my situation.

Suppose that you have ResourceDictionary in MyDictionary.xaml file.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Class="YourNamespace.MyDictionary">    
</ResourceDictionary>

You can add an x:Class attribute to the ResourceDictionary element and specify the fully qualified name of the code-behind class.

Let's create MyDictionary.xaml.cs with class MyDictionary (name can be different from the name of the xaml file).

public partial class MyDictionary
{
    public MyDictionary()
    {
        InitializeComponent();
    }
}

A class must be a partial class. The constructor must be added to the class and InitializeComponent method must be called. The InitializeComponent method will be automatically generated for the class if you set the x:Class attribute in MyDictionary.xaml

Now you can reference MyDictionary in MergedDictionaries

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <local:MyDictionary/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

If you add some DataTemplate into MyDictionary.xaml you can create event handlers in code-behind (handlers will be automatically generated by VS)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Class="YourNamespace.MyDictionary">    
    <DataTemplate x:Key="MyTemplate">
        <Button Click="Button_Click"/>
    </DataTemplate>
</ResourceDictionary>

Code-behind:

public partial class MyDictionary
{
    public MyDictionary()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // custom logic
        // edit another element, etc.
    }
}

If the class is inherited from the ResourceDictionary class then other resources can be accessed from the code-behind.

Example of usage of data template defined in MyDictonary:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <local:MyDictionary/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ContentControl ContentTemplate="{StaticResource MyTemplate}"/>
</Grid>

From my point of view the biggest advantages are that you can encapsulate logic into separated files (it's easy to maintain and add new features in big projects) and avoid referencing ResourceDictionaries by <ResourceDictionary Source="uri to your xaml file"/>.

like image 132
user2250152 Avatar answered Sep 19 '22 10:09

user2250152