在WPF拖放过程中如何更新拖动装饰器的位置? [英] How to update the position of a drag adorner during WPF drag-n-drop?

查看:143
本文介绍了在WPF拖放过程中如何更新拖动装饰器的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用装饰器来显示被拖动元素的鬼影 ...

I'm using an adorner to show a 'ghost' of the element being dragged...

var adornerLayer = AdornerLayer.GetAdornerLayer(topLevelGrid);
dragAdorner = new DragAdorner(topLevelGrid, itemToDrag);
adornerLayer.Add(dragAdorner);
dragAdorner.UpdatePosition(e.GetPosition(topLevelGrid));

DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move);

adornerLayer.Remove(dragAdorner);
itemToDrag = null;

...但是我找不到在此期间更新装饰器位置的好方法拖动。我得到的最接近的方法是在顶层网格上设置 AllowDrop = true 并为其提供DragOver处理程序...

...but I can't find a nice way to update the position of the adorner during the drag. The closest I've got is by setting AllowDrop="true" on the top level grid and giving it a DragOver handler...

private void TopLevelGrid_OnDragOver(object sender, DragEventArgs e)
{
 dragAdorner.UpdatePosition(e.GetPosition(topLevelGrid));
}

但这意味着我没有正确的 DragDropEffects 对光标的反馈,即,它始终显示 DragDropEffects.Move 光标,而不是 DragDropEffects.None 直到我超过了实际的下降目标。

But this means I don't get the proper DragDropEffects feedback on the cursor, i.e., it always shows the DragDropEffects.Move cursor instead of DragDropEffects.None until I'm over an actual drop target.

有人知道更好的方法来更新装饰位置吗?

Does anyone know a better way to update the adorner position?

推荐答案

(不幸的是,仅作为缓存版本提供)来自Bea Stollnitz的很老的博客帖子,其中的内容几乎涵盖了您的问题。

There's this (unfortunately only available as a cached version) pretty old blog post from Bea Stollnitz, which pretty much covers your question. It has a nice implementation of drag n drop with an adorner showing a "ghost image".

基本上,WPF中的拖放操作相当复杂,如果您愿意,可以通过拖放操作来显示n图像。一些自定义的DragAdorners-涉及添加一堆附加的依赖项属性,用于处理所有涉及的事件的设置,尤其是用于以不干扰投放代码的方式显示装饰器。

Basically drag and drop in WPF is quite the complicate procedure that - if you want some custom DragAdorners - involves adding a bunch of attached dependency properties for handling setup of all the events involved and especially for displaying the adorner in a way that doesn't interfere with the dropping code.

Bea的代码通过拥有一个帮助程序类来工作,该帮助程序类设置了拥有的 Window DragOver 事件处理程序和 AllowDrop 就在实际拖放操作之前,这样您就可以控制实际拖动源和放置目标之间的所有移动。

Bea's code works by having a helper class that sets the owning Window's DragOver event handler and AllowDrop right before the actual drag drop operation, that way you can control all the moving in between the actual drag source and the drop target.

这篇关于在WPF拖放过程中如何更新拖动装饰器的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆