Drag and Drop: Index of dropped item

I use Listboxwrapped inside ListBoxDragDropTarget(from the Silverlight toolkit). This Listboxca will be manually reordered by the user. However, the last element should always be located at the bottom Listboxand cannot be moved at all. I found a way to undo this last move of the element, but if I drag another element below this last element, it will be moved.

I can find the index of the item being dragged using this code in the DropListBox event :

object data = e.Data.GetData(e.Data.GetFormats()[0]);

ItemDragEventArgs dragEventArgs = data as ItemDragEventArgs;
SelectionCollection selectionCollection = dragEventArgs.Data as SelectionCollection;
if (selectionCollection != null)
{
    MyClass cw = selectionCollection[0].Item as MyClass;
    int idx = selectionCollection[0].Index.Value;   
}

However, this only gives me the index before the drag operation.

My question is this: is there a way to find out the new index of the dropped item? Thus, if index = last position of the list, I can move it to the extreme position.

Thanks in advance!

+2
source share
2 answers

Ok, I found a way to do this. I bound the event ItemDragCompleted ListBoxDragDropTargetand did the following:

private void dropTarget1_ItemDragCompleted(object sender, ItemDragEventArgs e)
{
    var tmp = (e.DragSource as ListBox).ItemsSource.Cast<MyClass>().ToList();

    SelectionCollection selectionCollection = e.Data as SelectionCollection;
    if (selectionCollection != null)
    {
        MyClass cw = selectionCollection[0].Item as MyClass;

        int idx = tmp.IndexOf(cw);
        if (idx == tmp.Count - 1)
        {
            tmp.Remove(cw);
            tmp.Insert(tmp.Count - 1, cw);

            MyListBox.ItemsSource = new ObservableCollection<MyClass>(tmp);
        }
}

}

Since DragSource represents a Listbox, with a new “layout” of elements, I can therefore check whether the element is at the end and move it in this case.

The only problem is that it causes flickering on the screen due to the element being discarded and then moved.

I am still open to any other (better) offer.

+4
source

. , ListBox , var point = args.GetPosition(myListBox);. ...

: , , , , silverlight, .

+2

All Articles