我想在树视图左侧的画布中绘制一些Bézier曲线,以显示项目之间的对应关系。我根据Josh Smith的教程实现了一个带延迟加载和mvvm的TreeView。
This is Minimum example how it should look.
要绘制我想要的线条,请使用canvas元素。所以我需要相对于TreeView的TreeViewItem的正确坐标。我使用Initialized Event获得了screenPosition。
private static void OnTreeViewItemIsInitialized(object sender, RoutedEventArgs e)
{
// Only react to the Selected event raised by the TreeViewItem
// whose IsSelected property was modified. Ignore all ancestors
// who are merely reporting that a descendant's Selected fired.
if (!Object.ReferenceEquals(sender, e.OriginalSource))
return;
TreeViewItem item = e.OriginalSource as TreeViewItem;
var parent = item.Parent;
var point = item.PointToScreen(new Point(0, 0));
var treeViewItemViewModel = item.DataContext as TreeViewItemViewModel;
if (treeViewItemViewModel != null)
treeViewItemViewModel.Coordinate = point;
}
我遇到了两个挑战:首先我只获得了屏幕位置,其次我不知道如何让TreeViewItem的父节点计算出正确的位置。
也许Iam以错误的方式解决我的问题?我确实找到了一些类似的例子,但没有相同TreeView的两个TreeViewItem之间的样条曲线的示例。