DataTemplate in a separate ResourceDictionary

12,205

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.

Share:
12,205

Related videos on Youtube

seveves
Author by

seveves

Software Engineer

Updated on June 04, 2022

Comments

  • seveves
    seveves almost 2 years

    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?