加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

.net – WPF拖放 – DragLeave Fire什么时候?

发布时间:2020-12-17 00:09:49 所属栏目:大数据 来源:网络整理
导读:从父级拖动到子控件时,我正在获取DragLeave事件.我只会期望在超出控制范围的情况下得到这个事件.我该如何实现? 请参考这个简单的示例应用程序. Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="h
从父级拖动到子控件时,我正在获取DragLeave事件.我只会期望在超出控制范围的情况下得到这个事件.我该如何实现?

请参考这个简单的示例应用程序.

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Height="50" >Hilight and Drag this text</TextBox>
        <Border BorderBrush="Blue" BorderThickness="2">
            <StackPanel AllowDrop="True" Name="Stack" >
                <Label >If I drag text across the gray line,Stack.DragLeave will fire.</Label>
                <Separator></Separator>
                <Label>I only expect to get this event when leaving the blue rectangle. </Label>
            </StackPanel>
        </Border>
        <TextBlock >Stack.DragLeave Count: <Label x:Name="countLabel" /></TextBlock>
    </StackPanel>
</Window>

并在代码背后

Class MainWindow

    Private Sub Stack_DragLeave(ByVal sender As Object,ByVal e As System.Windows.DragEventArgs) Handles Stack.PreviewDragLeave
        countLabel.Content = countLabel.Content + 1
    End Sub

End Class
我最近一直在使用我自己的应用程序,广泛使用WPF拖放,花了半天时间(调试,谷歌搜索,重新读取文档)完全相同的问题,我只能得出结论,有一个“未来增强“掩盖在WPF库中的某个地方.

这是我的解决方案:

protected virtual void OnTargetDragLeave(object sender,DragEventArgs e)
    {
        _dragInProgress = false;

        // It appears there's a quirk in the drag/drop system.  While the user is dragging the object
        // over our control it appears the system will send us (quite frequently) DragLeave followed 
        // immediately by DragEnter events.  So when we get DragLeave,we can't be sure that the 
        // drag/drop operation was actually terminated.  Therefore,instead of doing cleanup
        // immediately,we schedule the cleanup to execute later and if during that time we receive
        // another DragEnter or DragOver event,then we don't do the cleanup.
        _target.Dispatcher.BeginInvoke( new Action( ()=> {
                                if( _dragInProgress == false ) OnRealTargetDragLeave( sender,e ); } ) );
    }

    protected virtual void OnTargetDragOver(object sender,DragEventArgs e)
    {
        _dragInProgress = true;

        OnQueryDragDataValid( sender,e );
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读