WPF拖放 - 何时DragLeave火? [英] WPF Drag Drop - When Does DragLeave Fire?
问题描述
我是从父拖动到子控件的时候得到DragLeave事件。我只希望当一个控制的范围之外移动获得此事件。我怎样才能实现呢?
I am getting DragLeave Events when dragging from a parent to child control. I would only expect to get this event when moving outside the bounds of a control. How can I implement this?
请参考这个简单的示例应用程序。
Please refer to this simple sample application.
<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库中的某个地方埋了。
I've recently been working on my own application that is using WPF drag/drop extensively and after spending half a day (debugging, googling, re-reading documentation) on exactly same issue you are seeing, I could only conclude there's a "future enhancement" buried somewhere in WPF library.
这是我的解决办法:
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 );
}
这篇关于WPF拖放 - 何时DragLeave火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!