[UWP] Is there a reorder items event handler for ListView?

Leisn 6 Reputation points
2020-12-17T17:56:24.537+00:00

[UWP ListView]:
Recorder is work well on UI, but I need a way to listen the event.
I found that ObservableCollection has a CollectionChanged event handler, but when recorder completed , an Remove and Add actions emit in pairs.
This is difficult to handle.
I also found ObservableCollection has an action Move, why not just emit Move instead?

And for now, how can I handle that?

Universal Windows Platform (UWP)
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Yan Gu - MSFT 2,676 Reputation points
    2020-12-18T06:53:08.633+00:00

    Hello,

    Welcome to Microsoft Q&A

    When you reorder items in ListView control, you need to drag and drop an item. Dragging an item in ListView will be mapped to a Remove action of ObservableCollection, and dropping an item in ListView will be mapped to a Add action of ObservableCollection. Therefore, an Remove and Add actions emit in pairs when you reorder items by using drag-and-drop interation.

    If you want to listen the reorder event completion, you could add the following if statement to check if the CollectionChanged event handler is called the second time(CollectionChanged event handler completes the Add action) which marks the completion of the reorder.

    You could check the code as a sample:

    private ObservableCollection<string> collections;  
    private int count;  
    private bool flag = false;  
    public MainPage()  
    {  
        this.InitializeComponent();  
    
        collections = new ObservableCollection<string>();  
        collections.Add("cgedg");  
        collections.Add("detyh");  
        collections.Add("edgde");  
        collections.Add("asdfg");  
        collections.Add("bdedg");  
    
        collections.CollectionChanged += Collections_CollectionChanged;  
        count = collections.Count;  
    }  
    
    private void Collections_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)  
    {  
        if(count-1==collections.Count)  
        {  
            flag = true;  
        }  
        if(count==collections.Count&&flag)  
        {  
            //The reorder completes.  
            flag = false;  
        }  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.