在单独的 ResourceDictionary 中使用 DataTemplate

11

我知道与这个问题相关的话题很多,但我找不到完全适合我的问题的解决方案......也许没有呢?

目前我有一个包含导航的,允许用户在不同的屏幕之间切换。这些屏幕在我的的部分中定义为。

类似于这样:

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

好的,我的想法是将这些DataTemplate放在单独的XAML文件中,并将此文件链接到UserControl的资源部分。我是否真的需要在我的应用程序中全局公开这个新的XAML资源字典(将其添加到App.xaml资源中),或者还有其他更好的方法吗?

1个回答

15

不必将它设置为全局。只需在您的用户控件资源部分中声明资源字典,就像您在app.xaml中所做的那样。

<Control.Resources>
   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Control.Resources>
您可以使用相对文件路径 "..\Folder\Folder\Dictionary.xaml" 指向文件(如果需要的话)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接