在 WPF 中进行拖放时如何保持 UIELement 的形状
我有 2 个 StackPanel - 一个包含一个椭圆,另一个不包含任何内容。
我想将椭圆从一个堆栈面板拖动到另一个堆栈面板。实际的阻力和阻力drop 已经完成并且工作正常,但是我想知道如何在鼠标光标的尖端保持椭圆形状。
即使是 Windows 窗体示例也很好,我将能够对其进行调整。
I have 2 StackPanel
s - one containing an Ellipse
and the other one containing nothing.
I want to drag the Ellipse
from one stack panel to the other. The actual drag & drop is already done and works fine, however I'd like to know how to keep that Ellipse
shape at the tip of my mouse cursor.
Even a Windows Forms example would be nice, I'll be able to adapt it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想在拖动过程中添加拖动装饰器。我已经看到了几种不同的方法,但快速搜索会导致此行为< /a>,它的模式和我见过的一样好。一个具有代表性的示例是:
它们在
DragAdorner
类中很好地封装了拖动装饰的所有表示。 (实际上,我希望我能以这种方式完成)我还看到了用最初拖动的元素的视觉画笔构建装饰的技巧,但我认为到目前为止我最喜欢这种方法。
顺便说一句:在多次处理这个问题之后,我强烈建议将您的拖放代码包装成一种行为。否则你会发现它到处都是。使用 MVVM 进行正确的指挥可能相当棘手,但恕我直言,这是值得的。
You want to add your drag adorner during the drag. I've seen this done a few different ways, but a quick search leads to this behavior, which has as nice a pattern as I've seen. A representative sample is:
They wrap up all the presentation of the drag adornment nicely in the
DragAdorner
class. (I wish I'd done mine this way, actually)I've also seen tricks with building the adornment out of a Visual Brush of the originally dragged element, but I think I like this approach best so far.
BTW: After approaching this a few different times, I highly recommend wrapping your drag and drop code up into a behavior. Otherwise you wind up with it all over the place. Getting the commanding right with MVVM can be quite tricky, but it's worth it, IMHO.