Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Drag and Drop in WPF

by the3factory 3/5/2008 11:11:00 PM

To run the sample, open it in Visual Studio 2008. Then simply compile and run the application.

Several points on using the sample:

  • You can select multiple entries in the list by using Ctrl or Shift key together with the mouse.
  • In order to initiate the drag operation, you have to click on one of the selected items one more time and move the mouse while holding it down.
  • If at the end of "Drag" the mouse pointer will be over one of the selected items, no operation will be performed.
  • The selected items do not have to be contiguous at the start of drag operation, but after they are dropped they become contiguous.
  • The order of the dragged and drop items remains the same after the drop.
  •  

    Code description

     

    Here are some code excerpts. In function ListView1_PreviewMouseLeftButtonDown called in the beginning of drag operation we create a set of selected items (Dictionary with null values) and pass it to the DragDrop.DoDragDrop(...) function as data item:
                Dictionary shapes = new Dictionary();
    if (ListView1.SelectedItems.Count == 0)
    return;
    foreach(Shape shape in ListView1.SelectedItems)
    {
    shapes[shape] = null;
    }
    Shape currentShape = ListView1.Items[index] as Shape;
    // we do not intiate drag if the mouse descended on
    // a non-selected item during the beginning of drag
    if (!shapes.ContainsKey(currentShape))
    return;
    DragDrop.DoDragDrop(this.ListView1, shapes, allowedEffects);
    

     

    Function ListView1_Drop (the one implementing the drop operation) is slightly more complicated. First we record the list item into which the selected items are dropped:

    int index = this.GetCurrentIndex(e.GetPosition);
    ...
    Shape dropTargetShape = myShapes[index];
    
    Then we build a list of selected items to be dropped:
    List dropList = new List();
    foreach(Shape shape in myShapes)
    {
    if (!selectedShapes.ContainsKey(shape))
    continue;
    dropList.Add(shape);
    }
    
    We need this step in order to ensure that the dropped items are in the same order as they were originally. (In ListView.SelectedItems collection the items are stored in the order in which they are selected, not in the order in which they are in the ListView).

    Then we remove all the selected items from the collection myShapes (which is the collection of ListView items):

    foreach(Shape shape in dropList)
    {
    myShapes.Remove(shape);
    }
    
    Then we get the (possibly) new index of the drop target item within the modified collection:
    // find index of the drop target item after the removal
    // of the items to be dropped
    int selectIndex = myShapes.IndexOf(dropTargetShape);
    
    Finally we insert the items into the collection before the drop target item:
    for(int i = 0; i < dropList.Count; i++)
    {
    Shape shape = dropList[i];
    myShapes.Insert(i + selectIndex, shape);
    ...
    }
    

    Tags: ,

    WPF

    Related posts

    Sign up for PayPal and start accepting credit card payments instantly.


    Powered by BlogEngine.NET 1.2.0.0