UWP TreeView DragEnter and DragOver events do not

2019-08-17 11:55发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-17 12:27

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.
    }
} 
查看更多
登录 后发表回答