连接 wpf TreeView 中节点的虚线

发布于 2024-10-01 16:59:56 字数 863 浏览 1 评论 0原文

我正在寻找一种方法来绘制虚线来连接 wpf TreeView 中的节点。问题似乎是我使用 HierarchicalDataTemplate 而不是用 TreeViewItem 填充 TreeView

我找到了这个帖子: http://social. msdn.microsoft.com/forums/en-US/wpf/thread/30cb182c-9419-40bd-946e-87971515fb95/

女巫在填充 TreeViewItem 时很好地解决了这个问题,但是我的问题是,我如何用 HierarchicalDataTemplate 解决它?

我的模板看起来像这样:

<HierarchicalDataTemplate DataType = "{x:Type Team}" ItemsSource ="{Binding Path=Players">
   <TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>

<DataTemplate  DataType = "{x:Type Player}">
   <TextBlock Text="{Binding Path=Name}" />
</DataTemplate >

有关如何解决问题的任何解决方案或好的提示吗?

I am looking for a way to draw dotted lines to connect nodes in a wpf TreeView. The problem seems to be that i am using HierarchicalDataTemplate instead of populating the TreeView with TreeViewItems.

I have found this post:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/30cb182c-9419-40bd-946e-87971515fb95/

Witch solves it great in the case when populating it TreeViewItems but my question is, how would i solve it with HierarchicalDataTemplate?

My template look something like this:

<HierarchicalDataTemplate DataType = "{x:Type Team}" ItemsSource ="{Binding Path=Players">
   <TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>

<DataTemplate  DataType = "{x:Type Player}">
   <TextBlock Text="{Binding Path=Name}" />
</DataTemplate >

Any solution or good hints on how to solve the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

仅此而已 2024-10-08 16:59:56

在我看来,最简单的方法是将代码添加到 itemstyle 中

<HierarchicalDataTemplate.ItemContainerStyle>
   <Style TargetType="{x:Type TreeViewItem}">
      <!--your style-->
   </Style>
</HierarchicalDataTemplate.ItemContainerStyle>

The easiest ways, in my opinion, was to add the code in the itemstyle

<HierarchicalDataTemplate.ItemContainerStyle>
   <Style TargetType="{x:Type TreeViewItem}">
      <!--your style-->
   </Style>
</HierarchicalDataTemplate.ItemContainerStyle>
痞味浪人 2024-10-08 16:59:56

在我看来,解决此类问题的最简单方法是:

  1. 实现一个可以包含在 ItemTemplate 中的装饰器“ConnectingLineDecorator”。
  2. 定义继承的附加属性 ConnectingLineDecorator.ParentDecorator。
  3. 在 MeasureOverride 中执行 SetParentDecorator(Child, this) 以便后代装饰器可以找到其父级
  4. 在 ParentDecorator 属性的 PropetyChangedCallback 中添加代码,将每个装饰器注册到其 ParentDecorator,从而创建装饰器树。
  5. 每当装饰器的后代装饰器发生更改时,安排调度程序回调来重新计算哪些子装饰器需要显示行以及这些行应该有多长。
  6. 对每个装饰器使用装饰器来检测绝对位置变化。当装饰器相对于其父级移动时,安排相同的 Dispatcher 回调来重新计算
  7. OnRender 中的行,实际上使用最新的行数据渲染行(或者这可以在 Adorner 中完成,但如果是这样,您需要确保AdornerLayer 处于正确的级别)

这种方法不需要太多代码并且完全可靠,因为它不关心您正在使用哪种面板、滚动如何工作,甚至树的实际构建方式。装饰器只知道它的工作并执行它:它只是在它自己和它的祖先装饰器之间画线。

In my opinion the easiest way to solve this kind of problem is:

  1. Implement a Decorator "ConnectingLineDecorator" that you can include in the ItemTemplate.
  2. Define an inherited attached property ConnectingLineDecorator.ParentDecorator.
  3. In MeasureOverride do a SetParentDecorator(Child, this) so that descendant decorators can find their parent
  4. In the PropetyChangedCallback for the ParentDecorator property add code that registers each decorator with its ParentDecorator, creating a tree of decorators.
  5. Whenever a Decorator's descendant decorators change, schedule a Dispatcher callback to recompute which child decorators need to show lines and how long those lines should be.
  6. Use an Adorner with each decorator to detect absolute position changes. When a decorator moves with respect to its parent, schedule the same Dispatcher callback to recompute the lines
  7. In OnRender actually render the lines using the most recent line data (or this can be done in the Adorner, but if so you need to make sure the AdornerLayer is at the right level)

This approach doesn't take much code and is totally reliable, since it doesn't care what kind of panels you are using, how scrolling works, or even how the tree is actually built. The decorator just knows its job and does it: It just draws lines between itself and its ancestor decorators.

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