所以我设法做到这一点。我使用find祖先方法来获取treeviewitem对象,然后使用该对象的头部并将其转换为我用来创建树视图的NodeTreeVM对象。
private void nodeTree_Drop(object sender, DragEventArgs e)
{
//find the ancestor using the below method, this gets the TreeViewItem Object
TreeViewItem treeViewItem = FindAncestor<TreeViewItem>((DependencyObject)e.OriginalSource);
if (treeViewItem != null)
{
treeViewItem.Background = Brushes.White;
//Convert the header into the origional object
var droppedNode = (TreeNodeVM)treeViewItem.Header;
}
}
private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
{
// Search the VisualTree for specified type
while (current != null)
{
if (current is T)
{
return (T) current;
}
current = VisualTreeHelper.GetParent(current);
}
return null;
}
我希望这可以帮助别人,为更多的信息:)
看看了'msdn'示例代码请评论 - https://msdn.microsoft.com/en-us/库/ system.windows.dragdrop.dodragdrop.aspx – MethodMan 2015-02-11 18:28:48