Listbox drag-reorder : Index of the dropped item

后端 未结 2 1729
悲哀的现实
悲哀的现实 2021-01-03 13:37

I\'m using a Listbox wrapped inside a ListBoxDragDropTarget (from the Silverlight toolkit). This ListBox ca be manually reordered by t

相关标签:
2条回答
  • 2021-01-03 14:01

    I have this exact same problem. I think it may be possible to loop through the rectangles of the items in the ListBox and see if var point = args.GetPosition(myListBox); is within them. But I am hoping for an easier way...

    Edit: The problem with this though, is you don't get the gravity effect that is already built in to silverlight where only half of the above and half of the below rectangle are used to drop it into the list.

    0 讨论(0)
  • 2021-01-03 14:07

    Ok I found a way to do this. I bound the ItemDragCompleted event of the ListBoxDragDropTarget, and 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);
            }
    }
    

    }

    As the DragSource represents the Listbox, with the new "arrangement" of items, I can therefore check if the item is now located at the end, and move it in this case.

    The only problem left is that it causes a flicker on the screen, due to the item being dropped and then moved.

    I'm still open to any other (best) suggestion.

    0 讨论(0)
提交回复
热议问题