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

从父级拖动到子控件时,我正在获取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 );
    }

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...