覆盖数据绑定 (XML/XPath) TreeView 中特定节点的默认模板

发布于 2024-12-06 17:08:49 字数 861 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-12-13 17:08:49

DataTemplateHierarchicalDataTemplate 也有一个 DataType 属性。
当您删除x:Key并提供DataType时,这些模板是隐式的。因此,您可以将不同的模板定义为隐式模板,只要您不在初始 TreeView< 上提供 ItemTemplateItemTemplateSelector ,它们就会自动使用。 /代码>。

<HierachicalDataTemplate DataType="{x:Type ProprietaryFooService}">

就我个人而言,我尽量避免这种情况,因为这也意味着可以显示您的数据的其他控件也正在使用这些模板。我认为最好是使用 DataTemplateSelector,特别是当您处理需要以不同方式显示的相同类型时。

编辑:
抱歉,我没注意到您正在使用 Xpath。我想那是行不通的。我将把这个答案留在这里,但不能保证它适合您的需求。无论如何,也许它以另一种方式有所帮助。

DataTemplate and HierarchicalDataTemplate also have a DataType property.
When you remove the x:Key and supply a DataType, 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 a ItemTemplate or ItemTemplateSelector on the inital TreeView.

<HierachicalDataTemplate DataType="{x:Type ProprietaryFooService}">

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文