仅针对某些视图定位 DataTemplate
我在全局/共享 ResourceDictionary 中有一个 DataTemplate,如下所示,它针对 DataType:
<DataTemplate DataType="{x:Type foo:Bar}">
<!-- My DataTemplate visual tree goes here... -->
</DataTemplate>
此 DataTemplate 替换了我所有视图(UserControls/Windows)上的所有 foo:Bar 类型。我想要做的是仅将此模板应用于某些视图,保持其他视图不受此 DataTemplate 的影响。我可以将此 DataTemplate 复制到每个视图的资源部分,但我不想复制/粘贴 DataTemplate 的内容,这会导致维护麻烦。
I have a DataTemplate inside a global/shared ResourceDictionary like this which targets a DataType:
<DataTemplate DataType="{x:Type foo:Bar}">
<!-- My DataTemplate visual tree goes here... -->
</DataTemplate>
This DataTemplate replaces my all foo:Bar types on all my Views (UserControls/Windows). What I want to do is to apply this template to only certain views, keeping the other views are not affected by this DataTemplate. I can copy this DataTemplate to Resources sections of each of these view, but I don't want to copy/paste the contents of the DataTemplate which would result in maintenance headaches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在此处使用的内容称为隐式数据模板。而你要求的是一个明确的。要实现此目的,您可以使用显式资源键:
稍后在 xaml 中:
另一种解决方案是通过 在适当的控件/页面内合并字典。
我更喜欢第一种方法,因为它更容易维护(隐式样式更难跟踪)。
What you are using here is called implicit data template. And you are asking for an explicit one. To accomplish this you could use explicit resource key:
And later in xaml:
Another solution would be one resource dictionary (with implicit data templates) used via Merged Dictiories inside appropriate Control/Page.
I prefer the first approach, because it's easier to maintain (implicit styles are way harder to trace).