在 WPF 中拖动时更改光标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14342463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:02:33  来源:igfitidea点击:

Change cursor when dragging in WPF

wpfdrag-and-drop

提问by theqs1000

I'm using this great drag/drop framework: http://code.google.com/p/gong-wpf-dragdrop/I have two listboxes - A and B. When I drag from B to A, I want the mouse cursor to change as soon as the cursor is within the area of listbox A.

我正在使用这个很棒的拖放框架:http: //code.google.com/p/gong-wpf-dragdrop/我有两个列表框 - A 和 B。当我从 B 拖到 A 时,我想要鼠标一旦光标位于列表框 A 的区域内,光标就会改变。

I've almost got it. By using the IDropTarget interface as follows:

我已经差不多了。通过使用 IDropTarget 接口如下:

void IDropTarget.DragOver(DragOver drag)
{
 drag.Effects = DragDropEffects.Copy | DragDropEffects.Move;

 // some logic to determine if hovering over listbox A
 // ...

 if (hoveringOverListA)
 {
  ListBoxA.Cursor =  ((FrameworkElement) Application.Current.Resources["ListboxACursor"]).Cursor;
 }
}

The only problem is, while I'm dragging over the cursor that shows is the the operation not allowed one (the black circle with the line through it). As soon as I release the mouse, then I see my ListboxACursor appear. So it's like it's a delayed reaction, like it's waiting for me to Drop instead of doing it while I'm DragOver'ing.

唯一的问题是,当我拖动光标时,显示的是不允许的操作(黑色圆圈和穿过它的线)。一旦我松开鼠标,我就会看到我的 ListboxACursor 出现。所以这就像一个延迟的反应,就像它在等待我放下而不是在我 DragOver'ing 时做。

If anyone can see what is wrong with the code, I would greatly appreciate it. I have a feeling it might be to do with the DragDropEffects but it's mainly a hunch.

如果有人能看到代码有什么问题,我将不胜感激。我有一种感觉,这可能与 DragDropEffects 有关,但这主要是一种预感。

回答by Artfunkel

Answering this question because it's the top Google result.

回答这个问题是因为它是 Google 的最高结果。

protected override void OnGiveFeedback(System.Windows.GiveFeedbackEventArgs e)
{
    Mouse.SetCursor(Cursors.Hand);
    e.Handled = true;
}

This method should be defined on the drag source. It's critical that you flag the event as handled and that you DO NOT call base.OnGiveFeedback, as doing either will result in the default drag cursor overriding your changes.

这个方法应该在拖动源上定义。将事件标记为已处理并且不要调用 至关重要base.OnGiveFeedback,因为这样做会导致默认拖动光标覆盖您的更改。

Notice how I'm also using Mouse.SetCursorinstead of the more obvious FrameworkElement.Cursor. The latter is a persistent property of the drag source element, and it's unlikely that you actually want to change

注意我是如何使用Mouse.SetCursor而不是更明显的FrameworkElement.Cursor. 后者是拖拽源元素的持久属性,你不太可能真的想要改变

回答by Matthias

That is because Windows tries to use its own cursor to ensure a default look&feel. You can avoid this by explicitely disabling the default cursor. See GiveFeedback event in this tutorial

这是因为 Windows 尝试使用自己的光标来确保默认的外观和感觉。您可以通过显式禁用默认光标来避免这种情况。请参阅本教程中的GiveFeedback 事件

 private void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
 {
   e.UseDefaultCursors = e.Effect != DragDropEffects.Copy;
 }