DataTemplate in a separate ResourceDictionary

I know that there are many topics related to this issue, but I could not find a solution that is ideal for my problem ... maybe not?

At the moment, I have a UserControl that contains a navigation that allows the user to switch between different screens. These screens are defined in the Resources part of my UserControl as a 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 I want to do this in order to place these DataTemplates in a separate XAML file and link this file to a part of the UserControl resources. Should I really make this new XAML resource dictionary globally available in my application (adding it to App.xaml resources) or is there another / better way?

+7
source share
1 answer

No, you do not need to make it global. Just declare the resource dictionary in the user management resources section in the same way as in app.xaml.

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

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

+13
source

All Articles