Drag&Drop a clone of an object
-
2012年3月13日 下午 02:18
Hi everyone.
I need to do some dragging and dropping and came across a little problem.
I'm doing pretty much the same as seen in this code example (not my code, I just googled this to quickly show roughly what I'm doing):
public partial class Form1 : Form { private bool m_DragInProgress; public Form1() { InitializeComponent(); } private void listView1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (!m_DragInProgress && listView1.GetItemAt(e.Location.X, e.Location.Y) != null) { m_DragInProgress = true; listView1.DoDragDrop(listView1.GetItemAt(e.Location.X, e.Location.Y), DragDropEffects.Copy); } } else { m_DragInProgress = false; } } private void listView1_DragOver(object sender, DragEventArgs e) { if (m_DragInProgress) { Point l_point = listView1.PointToClient(new Point(e.X, e.Y)); ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y); if (l_item != null && l_item != listView1.SelectedItems[0]) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } } private void listView1_DragDrop(object sender, DragEventArgs e) { if (m_DragInProgress) { Point l_point = listView1.PointToClient(new Point(e.X, e.Y)); ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y); if (l_item != null && l_item != listView1.SelectedItems[0]) { if (listView1.SelectedItems[0].ImageIndex != -1) { l_item.ImageIndex = listView1.SelectedItems[0].ImageIndex; } else { l_item.ImageKey = listView1.SelectedItems[0].ImageKey; } } m_DragInProgress = false; } } }My problem is that I can't disconnect the UserControl from it's StackPanel before dropping it in another container, because I don't want it to disappear from its original location. DragDropEffects.Copy apparently doesn't do what it sounds like and so I tried a different approach.
I try to drag an exact clone of the UserControl from its StackPanel into the other container. For this I serialize the UserControl and deserialize it in a new instance of my UserControl using following method:
public static T Clone<T>(this T source) { // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } Stream stream = new MemoryStream(); using (stream) { Serializer.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)Serializer.Deserialize<T>(stream); } }
Problem is apparently instead of a duplicate I only get a reference to the original and so I still end up with an object which has the StackPanel as a parent. And if I disconnect my 'clone', the original is being disconnected aswell.
I'm sure this can be done much easier and better. Any ideas?
- 已移動 Neddy RenModerator 2012年3月15日 上午 02:43 (From:Visual C# Language)
所有回覆
-
2012年3月13日 下午 03:32
First, DragDropEffects.Copy doesn't do anything. It's a value. Your code can change its behavior according to the value of DragEventArgs.Effect.
Cloning an object by serializing and deserializing is a bad idea. You should implement a specific Clone method.
-
2012年3月15日 上午 02:54版主
For do dragdrop with copy-paste issue, see the following thread:
http://social.msdn.microsoft.com/Forums/en/winforms/thread/e886bff1-66e7-491f-b208-a4d2d4784183
Best Regards
Neddy Ren[MSFT]
MSDN Community Support | Feedback to us
- 已標示為解答 Neddy RenModerator 2012年3月28日 上午 06:06

