Skip to content

Fixed the drag and drop between ListViews#1898

Merged
marcelwgn merged 9 commits intomicrosoft:mainfrom
jpcassano:main
Jul 9, 2025
Merged

Fixed the drag and drop between ListViews#1898
marcelwgn merged 9 commits intomicrosoft:mainfrom
jpcassano:main

Conversation

@jpcassano
Copy link
Contributor

Description

The current drag and drop between ListViews continually duplicates the contact being dragged and does not remove the contact from the original list. This fix ensures that a contact will only show up in one of the ListViews at any given time.

Motivation and Context

Developers should have an example of a working drag and drop between two ListViews

How Has This Been Tested?

This logic has been manually tested to verify that a contact can be moved between ListViews.

Screenshots (if appropriate):

image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

…oved back and forth but never appear in both ListViews at the same time.
@jpcassano
Copy link
Contributor Author

@microsoft-github-policy-service agree

@niels9001 niels9001 requested review from Copilot and marcelwgn June 7, 2025 15:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the drag-and-drop logic between two ListViews so that a contact is moved (not duplicated) and removed from its original list.

  • Unified drag-start handling into ListView_DragItemsStarting and switched from string concatenation to JSON serialization.
  • Updated ListView_Drop to deserialize a list of Contact objects and correctly insert/move them.
  • Removed the old Target_DragItemsStarting handler and updated XAML to bind both ListViews to the new drag-start event.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
WinUIGallery/Samples/ControlPages/ListViewPage.xaml.cs Switched to JSON-based drag data, renamed handler, updated drop logic
WinUIGallery/Samples/ControlPages/ListViewPage.xaml Updated DragItemsStarting bindings to the new unified handler

string[] items = s.Split('\n');
foreach (string item in items)
var contactsToMoveAsJson = await e.DataView.GetTextAsync();
var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson);
Copy link

Copilot AI Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deserialization can return null, leading to a NullReferenceException in the subsequent foreach. Add a null check or default to an empty list before iterating.

Suggested change
var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson);
var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson) ?? new List<Contact>();

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@Zakariathr22 Zakariathr22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're currently using JsonSerializer.Serialize / Deserialize to pass drag data (List<Contact>) between ListView_DragItemsStarting and ListView_Drop. Since this drag-and-drop is strictly in-app, this adds unnecessary serialization overhead.

Use DataPackage.Properties instead of JSON

Ultimately, I defer to @marcelwgn’s opinion on this.

Comment on lines +115 to +116
var contactsToMoveAsJson = JsonSerializer.Serialize(contactsToMove);
e.Data.SetText(contactsToMoveAsJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var contactsToMoveAsJson = JsonSerializer.Serialize(contactsToMove);
e.Data.SetText(contactsToMoveAsJson);
e.Data.Properties["contacts"] = contactsToMove;

Comment on lines +137 to +138
var contactsToMoveAsJson = await e.DataView.GetTextAsync();
var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var contactsToMoveAsJson = await e.DataView.GetTextAsync();
var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson);
var contactsToMove = (List<Contact>)e.DataView.Properties["contacts"];

@jpcassano jpcassano closed this Jun 8, 2025
@marcelwgn
Copy link
Contributor

@jpcassano Thank you for creating this PR. Is it intentional you closed this PR or would you like to continue working on this?

@jpcassano jpcassano reopened this Jun 10, 2025
@jpcassano
Copy link
Contributor Author

I reopened the issue and updated the code to use the DataPackage properties. In my initial commit I saw that the original author chose to use StandardDataFormats.Text and it may not have been the best choice but I did not want to deviate much from that code while fixing the bug.

@marcelwgn
Copy link
Contributor

Thank you for making this contribution and adjusting the code as suggested! @Zakariathr22 the code looks good to me but would also like your opinion on this.

@marcelwgn
Copy link
Contributor

/azp run

@marcelwgn marcelwgn requested a review from Zakariathr22 July 9, 2025 13:09
Copy link
Contributor

@Zakariathr22 Zakariathr22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove the unused namespaces
Otherwise, it looks good to me

Co-authored-by: Zakaria Tahri <zakotahri@outlook.com>
@marcelwgn
Copy link
Contributor

/azp run

@marcelwgn marcelwgn enabled auto-merge (squash) July 9, 2025 14:41
@marcelwgn marcelwgn merged commit e82ebed into microsoft:main Jul 9, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants