简体   繁体   English

拖放winforms或wpf

[英]drag and drop in winforms or wpf

i would like to create a simple winforms or wpf application where i can drag and drop virtual "cards". 我想创建一个简单的winforms或wpf应用程序,我可以拖放虚拟“卡”。 this below is not exactly what i want to do, but it the closest thing that i found on the web to represent the user interface. 下面这不是我想要做的,但它是我在网络上发现的最接近用户界面的东西。

http://www.greenpeppersoftware.com/confluence/plugins/advanced/gallery-slideshow.action?imageNumber=1&pageId=24870977&decorator=popup&galleryTitle=Task+board+and+transitions http://www.greenpeppersoftware.com/confluence/plugins/advanced/gallery-slideshow.action?imageNumber=1&pageId=24870977&decorator=popup&galleryTitle=Task+board+and+transitions

so basically i want to have columns in the GUI where i can drag and drag from one to the other. 所以基本上我想在GUI中有列,我可以从一个拖动到另一个。

My questions are: 我的问题是:

  1. would this be easier in winforms or wpf 在winforms或wpf中这会更容易吗?
  2. where do i start? 我从哪里开始?

In both winForms and WPF dragging and dropping can be done in a similar way by working with the events on the target DragOver and Drop. 在winForms和WPF中,通过使用目标DragOver和Drop上的事件,可以以类似的方式完成拖放操作。

However with WPF you have other options. 但是使用WPF,您还有其他选择。 You will also be able to make the application look better by having a thumbnail as you drag (this is possible in winforms but harder to achieve). 通过在拖动时使用缩略图,您还可以使应用程序看起来更好(这在winforms中可能但更难实现)。

Have a look at this WPF sample it uses a helper class and think it does exactly what you need. 看看这个WPF示例,它使用了一个帮助程序类,并认为它完全符合您的需要。

I agree with John in that WinForms and WPF are quite close to one another wrt drag'n'drop. 我同意John的说法,因为WinForms和WPF之间的距离非常接近。 But WPF offers more of a "common base" for ItemsControl, allowing to implement more independent of the final UI elements used (ListBox, ListView, TreeView... can be easily switched). 但是WPF为ItemsControl提供了更多的“通用基础”,允许实现更多独立于最终使用的UI元素(ListBox,ListView,TreeView ......可以轻松切换)。 And obviously WPF allows much more fancy effects. 显然WPF允许更多花哨的效果。

I would strongly recommend this blog post: http://www.beacosta.com/blog/?p=53 both for some drag'n'drop basics and for a clean WPF drag'n'drop approach. 我强烈推荐这篇博文: http//www.beacosta.com/blog/? p = 53,既有一些拖拉的基础知识,也有一个干净的WPF拖拉方式。 It shows a nice implementation of a rather generic helper for drag'n'drop from/to WPF ItemsControls, I really like that "Insertion Adorner". 它显示了一个很好的实现一个相当通用的帮助器,用于从/向WPF ItemsControls拖拽,我真的很喜欢“插入式装配器”。 And I do like that the drag'n'drop code is nicely separated from the user control itself by using attached properties, which makes it much easier to use and maintain. 而且我确实喜欢通过使用附加属性将drag'n'drop代码与用户控件本身完美地分开,这使得它更易于使用和维护。

It would probably be slightly easier in WPF because of the Thumb control which provides easy to use built-in support for dragging. 由于Thumb控件提供易于使用的内置拖动支持,因此在WPF中可能会稍微容易一些。 (If I remember correctly, in WinForms you would need to handle the mouse events yourself, whereas the WPF Thumb does this for you and translates them into drag start, delta and end events.) (如果我没记错的话,在WinForms中你需要自己处理鼠标事件,而WPF Thumb会为你做这件事并将它们转换成拖动开始,增量和结束事件。)

However if you are much more familiar with one framework than the other than that would probably dwarf the difference that the Thumb control would make. 但是,如果您比其他框架更熟悉一个框架,那么可能会使Thumb控件产生的差异相形见绌。

You should also have a look around for toolkits/frameworks that could handle this for you -- I think they exist for both WinForms and WPF (not sure though). 您还应该查看可以为您处理此问题的工具包/框架 - 我认为它们同时适用于WinForms和WPF(不确定)。

A good way for darg and drop are explained as 解释darg和drop的好方法

Detect a drag as a combinatination of MouseMove and MouseLeftButtonDown 检测拖动作为MouseMove和MouseLeftButtonDown的组合

Find the data you want to drag and create a DataObject that contains the format, the data and the allowed effects. 找到要拖动的数据并创建包含格式,数据和允许的效果的DataObject。

Initiate the dragging by calling DoDragDrop() 通过调用DoDragDrop()启动拖动

Set the AllowDrop property to True on the elements you want to allow dropping. 在要允许删除的元素上将AllowDrop属性设置为True。

Register a handler to the DragEnter event to detect a dragging over the drop location. 将处理程序注册到DragEnter事件以检测拖放位置上的拖动。 Check the format and the data by calling GetDataPresent() on the event args. 通过在事件args上调用GetDataPresent()来检查格式和数据。 If the data can be dropped, set the Effect property on the event args to display the appropriate mouse cursor. 如果可以删除数据,请在事件参数上设置Effect属性以显示相应的鼠标光标。

When the user releases the mouse button the DragDrop event is called. 当用户释放鼠标按钮时,将调用DragDrop事件。 Get the data by calling the GetData() method on the Data object provided in the event args. 通过在事件args中提供的Data对象上调用GetData()方法来获取数据。

You can find the complete article here 你可以在这里找到完整的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM