简体   繁体   中英

Reorder ListView items: Event and Drag/Drop

My page contains two ListView elements, ListA and ListB. You can copy items (objects) from ListA to ListB by use of simple drag and drop. For that I use the Lists' DragItemsStarting and Drop events. That works just fine. Now you can reorder the items in ListB. All required properties are set (reorder, drag and drop) and this works as well.

But now I want to react on the resorting, but I didn't find an event I could listen to. So I thought it might be possible to use the drag/drop events on the same list to get to know when the user has changed the psoition of an item.

So how can I recognize that the items of ListB have been reorderd?

My code (with the drag and drop approach):

private void ListB_DragItemsStarting(object sender, DragItemsStartingEventArgs e) {
            var item = ((FeedItem)e.Items[0]);
            e.Data.RequestedOperation = DataPackageOperation.Move;
            e.Data.SetDataProvider("FeedItem", request => request.SetData(item));
        }

private async void ListB_Drop(object sender, DragEventArgs e) {
            DataPackageView view = e.Data.GetView();
            if (view.Contains("FeedItem") && view.RequestedOperation == DataPackageOperation.Copy) {
                //item from ListA 
            }
            if (view.Contains("FeedItem") && view.RequestedOperation == DataPackageOperation.Move) {
                //item from ListB
            }
        }

Ok, problem solved :)

To use drag and drop on the same UI element (in this case a ListView) you need to disable the CanReorderItems option. Then the code I provided above will work.

ListB.CanReorderItems = false;

But when you wish to keep the option to reorder the items in ListB (actually my original question) you can subscribe to the follwoing event:

view.VectorChanged += viewVectorChanged;

...

private void viewVectorChanged(Windows.Foundation.Collections.IObservableVector<object> sender, Windows.Foundation.Collections.IVectorChangedEventArgs @event) {

}

Since my list is bound to a CollectionViewSource this works fine for me. Otherwise ListView should have a similar event that you could subscribe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM