gpt4 book ai didi

.net - WPF 拖放 - DragLeave 何时触发?

转载 作者:行者123 更新时间:2023-12-03 20:33:08 25 4
gpt4 key购买 nike

从父控件拖动到子控件时,我收到 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

enter image description here

最佳答案

我最近一直在开发自己的应用程序,该应用程序广泛使用 WPF 拖放,并且在您所看到的完全相同的问题上花了半天时间(调试、谷歌搜索、重新阅读文档)之后,我只能得出结论:“ future 增强”埋在 WPF 库的某个地方。

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 quite frequently send us DragLeave events, 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.



这是我的解决方案:
protected virtual void OnTargetDragLeave(object sender, DragEventArgs e)
{
_dragInProgress = false;

_target.Dispatcher.BeginInvoke( new Action( ()=>
{
if( _dragInProgress == false ) OnRealTargetDragLeave( sender, e );
} ) );
}

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

OnQueryDragDataValid( sender, e );
}

关于.net - WPF 拖放 - DragLeave 何时触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5447301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com