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 TreeViewItem
s.
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 TreeViewItem
s 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:
- Implement a Decorator "ConnectingLineDecorator" that you can include in the ItemTemplate.
- Define an inherited attached property ConnectingLineDecorator.ParentDecorator.
- In MeasureOverride do a
SetParentDecorator(Child, this)
so that descendant decorators can find their parent - In the PropetyChangedCallback for the ParentDecorator property add code that registers each decorator with its ParentDecorator, creating a tree of decorators.
- 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.
- 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
- 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.
精彩评论