Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add more than one resource to a XAML window?

Tags:

I have a little problem right now and I don't know how to fix it. I want to add two resources to a window. One is a XAML File style resource, the other a ValueConverter Class.

Both of them work if I use only one resource at a time:

 <Window.Resources>
    <ResourceDictionary Source="Resources\MyStyles.xaml" />
 <Window.Resources>

or

<Window.Resources>
    <local:MarginConverter x:Key="adjustMargin"/>
</Window.Resources>

But if I try something like this:

<Window.Resources>
    <local:MarginConverter x:Key="adjustMargin"/>
    <ResourceDictionary Source="Resources\MyStyles.xaml" />
</Window.Resources>

I get the message the resource is already been set and can not set twice.

I have no idea how to get this done. Is there something like a resource group?

like image 596
TalkingCode Avatar asked Mar 04 '09 12:03

TalkingCode


People also ask

What is resource in XAML?

A resource is an object that can be reused in different places in your app. Examples of resources include brushes and styles. This overview describes how to use resources in Extensible Application Markup Language (XAML). You can also create and access resources by using code.

What is window resources in WPF?

Windows Presentation Foundation (WPF) resources provide a simple way to reuse commonly defined objects and values. Resources in WPF allow you to set the properties of multiple controls at a time. For example you can set the background property on several elements in a WPF application using a single resource.


1 Answers

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\MyStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <local:MarginConverter x:Key="adjustMargin"/>
    </ResourceDictionary>
</Window.Resources>
like image 196
Kent Boogaart Avatar answered Oct 21 '22 07:10

Kent Boogaart