none
Drag&Drop a clone of an object

    Pregunta

  • 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?

    • Cambiado Neddy Ren jueves, 15 de marzo de 2012 2:43 (From:Visual C# Language)
    martes, 13 de marzo de 2012 14:18

Respuestas

Todas las respuestas