获得从在树视图的Drop事件一个TreeViewItem当选择不同的节点 [英] Getting a TreeViewItem from the Drop event in a TreeView when a different node is selected
问题描述
我有一个从ListBox拖放到TreeView中的问题,我遇到的问题是,我只能看到在Drop事件下从TreeView发件人返回的"IsSelected"节点,这是因为我选择了一个属性从TreeViewItem的列表框中,并将其拖到另一个TreeViewItem中.
I have a drag and drop from a ListBox into a TreeView, the problem I have is that I can only see the "IsSelected" node that is returned from the TreeView sender under the Drop event, this is because I am selecting a property from the listbox of a TreeViewItem and dragging it into another TreeViewItem.
我希望这是有道理的.
我无法从删除的" TreeViewItem中获取数据,我目前有这些方法,但无法获取将ListBoxItem放入其中的TreeViewItem.
I can't get the data from the "dropped" TreeViewItem, I currently have these methods but I can't get the TreeViewItem I drop the ListBoxItem into.
private void nodeTree_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("copyProperty"))
{
BasePropertyTypeVM dragged = e.Data.GetData("copyProperty") as BasePropertyTypeVM;
}
}
private void NodeTree_OnDragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("copyProperty") ||
sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
private void NodeTree_OnDragOver(object sender, DragEventArgs e)
{
TreeViewItem treeViewItem = FindAncestor<TreeViewItem>((DependencyObject) e.OriginalSource);
if (treeViewItem != null)
{
treeViewItem.Background = Brushes.Blue;
}
}
private void NodeTree_OnDragLeave(object sender, DragEventArgs e)
{
TreeViewItem treeViewItem = FindAncestor<TreeViewItem>((DependencyObject) e.OriginalSource);
if (treeViewItem != null)
{
treeViewItem.Background = Brushes.White;
}
}
推荐答案
所以我设法做到了.我使用find祖先方法获取treeviewitem对象,然后使用该对象的标头并将其转换为用于创建树视图的NodeTreeVM对象.
So I managed to do this. I used the find ancestor method to get the treeviewitem object and then used the header from this object and converted this to the NodeTreeVM object I had used to create the tree view.
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;
}
我希望这对其他人也有帮助,请发表评论以获取更多信息:)
I hope this also helps someone else, please comment for more info :)
这篇关于获得从在树视图的Drop事件一个TreeViewItem当选择不同的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!