首先让HierachichalDataTemplate显示儿童。(Make HierachichalDataTemplate display children first. (Post

编程入门 行业动态 更新时间:2024-10-19 21:40:27
首先让HierachichalDataTemplate显示儿童。(Make HierachichalDataTemplate display children first. (Post-order (LRN)))

我想显示我当前元素的路径,如下所示:

Node1 > Node1_1 > Node1_1_1

路径保存在一个Parent元素链中,例如上面给出的例子:

Node1_1_1.Parent == Node1_1 Node1_1.Parent == Node1

我认为最简单的方法来归档这个结果是通过使用HierachichalDataTemplate ,它首先显示子> Node.Name ,然后将其本身作为> Node.Name 。 从Node1_1_1元素开始。

问题是HierachichalDataTemplate显示当前的DataContext ,然后显示子项。 我基本上想要归档我的Parent -tree的后序遍历。

有什么方法可以归档吗? 我曾想过重新定义HierachichalDataTemplate的模板,但我对WPF还不是很了解,但还没有将其归档。

I want to display the path to my current element, like so:

Node1 > Node1_1 > Node1_1_1

The path is saved in a chain of Parent elements, for the example given above:

Node1_1_1.Parent == Node1_1 Node1_1.Parent == Node1

I thought the easiest way to archive this result is by using a HierachichalDataTemplate which first displays the children and then itself as > Node.Name. Starting from the Node1_1_1 element.

The problem is the HierachichalDataTemplate displays the current DataContext first and then the children. I basically want to archive post-order traversal of my Parent-tree.

Is there any way to archive that? I thought about redefining the template of the HierachichalDataTemplate somehow, but I don't know enough about WPF yet to archive this myself.

最满意答案

我不完全确定“显示路径”的含义,但我想你可以在树视图中选择一个项目,并在Windows资源管理器中的单独文本框控件中显示其路径。 如果是这种情况,您需要跟踪所选项目,并在所选项目发生更改时解决其父级关系至根目录。

也就是说,viewmodel中的每个项目都需要像这样实现一个Parent接口(假设每个viewmodel项目实现了一个IItem接口,如下所示: https : //github.com/Dirkster99/InplaceEditBoxLib/blob/1a25512c7a5c4611809162d625ff9137a4ccb456/source/Solution/SolutionLib /Interfaces/IParent.cs )

public interface IParent { IItem Parent { get; } }

这个接口使您能够通过使用while循环浏览树根来累积路径,如下所示:

IItem node = MyTree.SelectedItem; string path = MyTree.SelectedItem.Name; //Assuming you have a SelectedItem while (node.Parent != null) { path = path.Parent.Name + " > " + path; }

I am not completely sure what you mean by "displaying the path" but I guess you want to be able to select an item in your treeview and shows its path in a seperate textbox control like in the Windows Explorer. If thats the case you need to keep track of the selected item and resolve its parent relationship up to the root whenever the selected item changed.

That is, every item in your viewmodel needs to implement a Parent interface like this (assuming every viewmodel item implements an IItem interface as shown here: https://github.com/Dirkster99/InplaceEditBoxLib/blob/1a25512c7a5c4611809162d625ff9137a4ccb456/source/Solution/SolutionLib/Interfaces/IParent.cs)

public interface IParent { IItem Parent { get; } }

This interface enables you to accumulate a path by browsing towards a trees root using a while loop like this:

IItem node = MyTree.SelectedItem; string path = MyTree.SelectedItem.Name; //Assuming you have a SelectedItem while (node.Parent != null) { path = path.Parent.Name + " > " + path; }

更多推荐

本文发布于:2023-07-26 15:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:儿童   HierachichalDataTemplate   display   Post   children

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!