Search code examples
xamllistviewuwpwindows-runtime

Why do I not get DragItemsCompleted and DragItemsStarting events when CanReorderItems is set to true on a UWP ListView?


I have a UWP XAML <ListView> that I want users to rearrange via drag and drop. The CanReorderItems property on ListViewBase provides that functionality and only requires a few properties:

<ListView ItemsSource="{x:Bind Items}"
    CanReorderItems="True"
    AllowDrop="True"
    DragItemsStarting="ListView_DragItemsStarting"
    DragItemsCompleted="ListView_DragItemsCompleted">
    <!-- ... -->
</ListView>

This successfully lets me drag and drop items within the ListView and fires CollectionChanged events on the ItemsSource (Remove then Add). However, it does not fire the DragItemsStarting and DragItemsCompleted events.

These events let me handle dragging atomically, rather than relying on two CollectionChanged events from the ItemsSource.

How do I get these events to fire?

Disclaimer: I work for Microsoft.


Solution

  • ListViews do not fire DragItemsStarting and DragItemsCompleted events unless CanDragItems is set to true on the ListView:

    <ListView ItemsSource="{x:Bind Items}"
        CanDragItems="True"
        CanReorderItems="True"
        AllowDrop="True"
        DragItemsStarting="ListView_DragItemsStarting"
        DragItemsCompleted="ListView_DragItemsCompleted">
        <!-- ... -->
    </ListView>
    

    Once you add this property, you should find that these events will fire.