wpf 单独的 ResourceDictionary 中的 DataTemplate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13246602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTemplate in a separate ResourceDictionary
提问by seveves
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 UserControlwhich holds a navigation which allows the user to switch between different screens. These screens are defined in the Resourcespart of my UserControlas DataTemplate.
目前我有一个UserControl导航,允许用户在不同的屏幕之间切换。这些屏幕在Resourcesmy UserControlas的部分中定义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?
好的,我想做的是将这些 DataTemplates 放在单独的 XAML 文件中,并将此文件链接到 UserControl 的资源部分。我真的必须在我的应用程序中全局使用这个新的 XAML 资源字典(将它添加到 App.xaml 资源)还是有其他/更好的方法?
回答by Rafal
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.
不,您不必使其成为全球性的。只需在您的用户控制资源部分声明资源字典,就像在 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.
"..\Folder\Folder\Dictionary.xaml"如果需要,您可以使用相对文件路径指向文件。

