DataTemplate位于单独的ResourceDictionary中

sev*_*ves 7 wpf resources xaml binding datatemplate

我知道有很多与此问题相关的主题,但我找不到适合我的问题的解决方案......也许没有?

目前我有一个UserControl导航,允许用户在不同的屏幕之间切换.这些屏幕在定义Resources我的一部分UserControl作为DataTemplate.

像这样的东西:

<DataTemplate TargetType={x:Type vm:ViewModel1}>
    ...
</DataTemplate>
<DataTemplate TargetType={x:Type vm:ViewModel2}>
    ...
</DataTemplate>
<DataTemplate TargetType={x:Type vm:ViewModel3}>
    ...
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

好的,我想做的是将这些DataTemplates放在一个单独的XAML文件中,并将此文件链接到UserControl的资源部分.我是否真的必须在我的应用程序中全局提供这个新的XAML资源字典(将其添加到App.xaml资源中)还是有其他/更好的方法?

Raf*_*fal 14

不,你不必让它全球化.只需在app.xaml中以与在app.xaml中相同的方式声明用户控制资源部分中的资源字典.

<Control.Resources>
   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Control.Resources>
Run Code Online (Sandbox Code Playgroud)

"..\Folder\Folder\Dictionary.xaml"如果需要,可以使用相对文件路径指向文件.