Repository navigation
[Windows][CV2][Net11] Fix CollectionView exception thrown when ItemsSource is set to Null - #39554
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39554Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39554" |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 Gate analysis — click to expand✅ Passed. The selected verification satisfied the gate's passing conditions. Unmatched, skipped, or inconclusive groups are not passing evidence. 📱 Device tests
Execution log excerptsWithout fix With fix PR head: ed4bc858. Without-fix baseline: e6d22838. |
There was a problem hiding this comment.
🔵 Needs a closer look
The claimed cleanup deferral remains unimplemented, requiring human confirmation of WinUI lifecycle safety.
1 open issue
What changed in this PR
This PR addresses Windows CollectionView2 exceptions when clearing and restoring ItemsSource, targeting issue #39552.
Changes:
- Assigns a null native source when the underlying source is null.
- Preserves WinUI’s default item template and removes a redundant native source reset.
- Adds device tests for repeated clear-and-restore cycles, with and without a MAUI item template.
| File | Description |
|---|---|
| src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs | Tests source transitions, empty-view visibility, and default-template preservation. |
| src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.Windows.cs | Adjusts null-source assignment, template preservation, and cleanup. |
Review effort: Balanced
|
|
||
| PlatformView.ItemsSource = _collectionViewSource?.View; | ||
| // A CollectionViewSource with no Source does not provide a usable WinUI view. | ||
| PlatformView.ItemsSource = _collectionViewSource?.Source is null ? null : _collectionViewSource.View; |
This comment has been minimized.
This comment has been minimized.
Preserve the null-source clear/restore regression theory alongside the independent upstream header BindingContext regression test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Resolved and pushed the conflict with the current The only conflict was overlapping test insertions in Validation: all eight selected managed CollectionView/ItemsView unit tests passed with native platform targets disabled; |

Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Issue Details:
On Windows, changing a CollectionView’s ItemsSource from a populated collection to null, or restoring a populated collection afterward, can throw “The parameter is incorrect.”
Root cause:
The handler assigned CollectionViewSource.View even when its underlying Source was null.
Cleanup cleared the old collection source while WinUI still referenced it.
When no MAUI ItemTemplate was supplied, the handler also cleared WinUI’s automatically generated default template. This affected subsequent source restoration.
Description of Change
Updated ItemsViewHandler2.Windows.cs:
Issues Fixed
Fixes #39552
Tested the behavior in the following platforms.
Before.mp4
After.mp4