【问题标题】:DataTemplate in a separate ResourceDictionary单独的 ResourceDictionary 中的 DataTemplate
【发布时间】:2012-10-26 03:27:58
【问题描述】:

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

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

类似的东西:

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

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

【问题讨论】:

    标签: wpf xaml binding resources datatemplate


    【解决方案1】:

    不,您不必将其设为全球性。只需像在 app.xaml 中一样,在您的用户控制资源部分声明资源字典。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-03
      • 2016-03-26
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多