开发者

Dotted lines to connect nodes in a wpf TreeView

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.ms开发者_Go百科dn.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?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜