Search code examples
c#listviewuwpdrag-and-dropdraggable

UWP - Is there any way to change the mouse cursor when exiting ListView without the event being triggered during drag and drop?


I have a draggable ListView that changes the mouse cursor on PointerEntered, PointerPressed and DragItemsStarting events. The problem is every time I perform drag and drop I always trigger PointerExited which resets the cursor back to arrow.

If it is impossible to not trigger PointerExited, what other ways can I do to reset the cursor back to arrow when exiting the Listview?


Solution

  • We can judge the event itself when the PointerExited event is triggered.

    PointerExited is indeed triggered when you drag the ListViewItem, but PointerRoutedEventArgs.OriginalSource is different from moving the pointer normally. This is the basis of our judgment.

    Try this:

    private void ListView_PointerExited(object sender, PointerRoutedEventArgs e)
    {
        if(e.OriginalSource is ListViewItemPresenter)
        {
            return;
        }
        Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
    }
    

    When dragging, the source that triggers the PointerExited event is ListViewItemPresenter. Through this judgment, Cursor can work as expected.

    Best regards.