Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTemplate in a separate ResourceDictionary

I know there a lot of topics related to this question but I could not find a solution that fits perfectly for my problem ... maybe there is none?

At the moment I have a UserControl which holds a navigation which allows the user to switch between different screens. These screens are defined in the Resources part of my UserControl as DataTemplate.

Something like that:

<DataTemplate TargetType={x:Type vm:ViewModel1}>
    ...
</DataTemplate>
<DataTemplate TargetType={x:Type vm:ViewModel2}>
    ...
</DataTemplate>
<DataTemplate TargetType={x:Type vm:ViewModel3}>
    ...
</DataTemplate>

Ok and what I wanna do is to place these DataTemplates in a separate XAML file and link this file to the UserControl's resources part. Do I really have to make this new XAML Resource Dictionary globally available in my application (adding it to the App.xaml resources) or is there another/better way?

like image 508
seveves Avatar asked Nov 06 '12 07:11

seveves


1 Answers

No you do not have to make it global. Simply declare resource dictionary in your user control resources section just the same way as you did in app.xaml.

<Control.Resources>
   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Control.Resources>

You can point to file using relative file path "..\Folder\Folder\Dictionary.xaml" if you need to.

like image 98
Rafal Avatar answered Nov 19 '22 16:11

Rafal