UWP TreeView DragEnter and DragOver events do not execute

独自空忆成欢 提交于 2019-12-11 17:06:25

问题


It is very important that I can track which item is dragged in UWP TreeView and onto which item it is dropped. For now, I can get only item that is dragged. But I can not detect over which item it is dragged over or over which item it is dropped. Also it would be also good to know as preview which item is dropped onto so I can do more actions (for example cancel dropping on certain items).

Here is my extended control:

    public class MyTreeView : TreeView
{
    public MyTreeView()
    {
        this.DragItemsStarting += MyTreeView_DragItemsStarting; //execute ok
        this.DragItemsCompleted += MyTreeView_DragItemsCompleted; //execute ok

        this.DragEnter += MyTreeView_DragEnter; //does not execute?
        this.Drop += MyTreeView_Drop; //does not execute?
        this.DragOver += MyTreeView_DragOver; //does not execute?
    }

    //...
}

In the xaml:

<localdata:MyTreeView 
            x:Name="treeview" Grid.Row="2" ItemsSource="{Binding storageFolders,Mode=OneWay}" 
            Style="{StaticResource TreeViewStyle1}"
            ItemTemplateSelector="{StaticResource ExplorerItemTemplateSelector}"
            SelectedItem="{Binding fileObject}"
            SelectedIndex="{Binding IndexObj, Mode=TwoWay}"
            >             
        </localdata:MyTreeView>

回答1:


It is by design , the DragOver will be invoked when other TreeView item hover current TreeView. If you want to realize cancel feature, you could judge if current TreeViewNode is correct for DragItems in the DragItemsCompleted event handler like the following .

private void ListControl_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
    foreach (var item in args.Items)
    {

        var node = TreeDataBound.NodeFromContainer(TreeDataBound.ContainerFromItem(item));          
        var parent = node.Parent;

      //do some stuff judge the parent.
    }
} 


来源:https://stackoverflow.com/questions/56425746/uwp-treeview-dragenter-and-dragover-events-do-not-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!