从父级拖动到子控件时,我正在获取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库中的某个地方.
原文链接:https://www.f2er.com/vb/255701.html这是我的解决方案:
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 ); }