Fixed the drag and drop between ListViews#1898
Conversation
…oved back and forth but never appear in both ListViews at the same time.
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
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_DragItemsStartingand switched from string concatenation to JSON serialization. - Updated
ListView_Dropto deserialize a list ofContactobjects and correctly insert/move them. - Removed the old
Target_DragItemsStartinghandler 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); |
There was a problem hiding this comment.
Deserialization can return null, leading to a NullReferenceException in the subsequent foreach. Add a null check or default to an empty list before iterating.
| var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson); | |
| var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson) ?? new List<Contact>(); |
There was a problem hiding this comment.
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.
| var contactsToMoveAsJson = JsonSerializer.Serialize(contactsToMove); | ||
| e.Data.SetText(contactsToMoveAsJson); |
There was a problem hiding this comment.
| var contactsToMoveAsJson = JsonSerializer.Serialize(contactsToMove); | |
| e.Data.SetText(contactsToMoveAsJson); | |
| e.Data.Properties["contacts"] = contactsToMove; |
| var contactsToMoveAsJson = await e.DataView.GetTextAsync(); | ||
| var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson); |
There was a problem hiding this comment.
| var contactsToMoveAsJson = await e.DataView.GetTextAsync(); | |
| var contactsToMove = JsonSerializer.Deserialize<List<Contact>>(contactsToMoveAsJson); | |
| var contactsToMove = (List<Contact>)e.DataView.Properties["contacts"]; |
|
@jpcassano Thank you for creating this PR. Is it intentional you closed this PR or would you like to continue working on this? |
|
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. |
|
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. |
|
/azp run |
Zakariathr22
left a comment
There was a problem hiding this comment.
Just remove the unused namespaces
Otherwise, it looks good to me
Co-authored-by: Zakaria Tahri <zakotahri@outlook.com>
|
/azp run |
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):
Types of changes