覆盖数据绑定 (XML/XPath) TreeView 中特定节点的默认模板
TL;DR 前面:我想对 WPF TreeView 中除少数特定节点之外的所有节点使用“默认”HierarchicalDataTemplate。这些节点来自 XMLDocument,直到运行时才完全可知。有办法做到这一点吗?
在运行时,我正在分析系统的特定部分,并构建一个 XML 文档,然后将其绑定到 TreeView,如下所示:
MyTree.DataContext = MyXMLDocument;
这是我的 TreeView 的 WPF 声明:
<TreeView x:Name="MyTree" ItemsSource="{Binding Mode=OneWay, XPath=/Analysis}" ItemTemplate="{StaticResource GenericElementWithChildren}"/>
模板的开头如下...
<HierarchicalDataTemplate x:Key="GenericElementWithChildren" ItemsSource="{Binding XPath=child::node()}">
例如,我可能有一些关于我刚刚运行的分析的不同方面的长 XML 文档,并且我希望某些特定元素(例如“Disk”或“Proprietary Foo Service”)有一个特殊的模板,因为它应该很好地显示,而其他所有内容都只是获取通用模板。
我考虑过使用 DataTemplateSelector 但似乎必须有更好的方法。我什至不确定如何为 XML 做到这一点。你们中是否有聪明人可以传授一些智慧,或者我是否一直在思考如何针对 XML 编写 DataTemplateSelector?
TL;DR up front: I would like to use a "default" HierarchicalDataTemplate for all but a specific few nodes in a WPF TreeView. These nodes come from an XMLDocument and are not fully known until runtime. Is there a way to do this?
At runtime, I am analyzing specific parts of a system, and building an XML document that I'm then binding to a TreeView like so:
MyTree.DataContext = MyXMLDocument;
This is my WPF declaration of the TreeView:
<TreeView x:Name="MyTree" ItemsSource="{Binding Mode=OneWay, XPath=/Analysis}" ItemTemplate="{StaticResource GenericElementWithChildren}"/>
The template starts like this...
<HierarchicalDataTemplate x:Key="GenericElementWithChildren" ItemsSource="{Binding XPath=child::node()}">
So for example, I might have some long XML document about different aspects of the analysis I just ran and I'd like some particular element like "Disk" or "Proprietary Foo Service" to have a special template because it should be displayed nicely, while everything else just gets a generic template.
I've thought about using a DataTemplateSelector but it seems like there must be a better way. I'm not even sure how I'd do that for XML. Do any of you smarter folks have any wisdom to impart, or am I stuck figuring out how to write a DataTemplateSelector against XML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataTemplate
和HierarchicalDataTemplate
也有一个DataType
属性。当您删除
x:Key
并提供DataType
时,这些模板是隐式的。因此,您可以将不同的模板定义为隐式模板,只要您不在初始TreeView< 上提供
ItemTemplate
或ItemTemplateSelector
,它们就会自动使用。 /代码>。就我个人而言,我尽量避免这种情况,因为这也意味着可以显示您的数据的其他控件也正在使用这些模板。我认为最好是使用 DataTemplateSelector,特别是当您处理需要以不同方式显示的相同类型时。
编辑:
抱歉,我没注意到您正在使用 Xpath。我想那是行不通的。我将把这个答案留在这里,但不能保证它适合您的需求。无论如何,也许它以另一种方式有所帮助。
DataTemplate
andHierarchicalDataTemplate
also have aDataType
property.When you remove the
x:Key
and supply aDataType
, these Templates are implicit. So you can define your different templates as implicit and they will be used automatically, as long as you don't supply aItemTemplate
orItemTemplateSelector
on the initalTreeView
.Personally i try to avoid that, because this also means that other controls that can show your data are using these Templates aswell. Imo the best would be to use a
DataTemplateSelector
, especially when you deal with the same types that you need to show in different ways.Edit:
Sorry i missed that you are using Xpath. I guess it will not work there. I will leave this answer here, but can't guarantee that it suits your needs. Maybe it helps in another way anyway.