DataTemplate in a separate ResourceDictionary
Asked Answered
F

1

11

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?

Fabre answered 6/11, 2012 at 7:49 Comment(0)
T
15

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.

Tangier answered 6/11, 2012 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.