Skip to content

Fix CollectionView record struct selection on Windows#33488

Merged
kubaflo merged 1 commit intodotnet:inflight/currentfrom
jeremy-visionaid:collectionview-value-type-selection
Mar 25, 2026
Merged

Fix CollectionView record struct selection on Windows#33488
kubaflo merged 1 commit intodotnet:inflight/currentfrom
jeremy-visionaid:collectionview-value-type-selection

Conversation

@jeremy-visionaid
Copy link
Copy Markdown
Contributor

Description of Change

Compare collection items with value equality

Issues Fixed

Fixes: #33487

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jan 13, 2026
@jeremy-visionaid
Copy link
Copy Markdown
Contributor Author

@hartez WDYT?

@jeremy-visionaid jeremy-visionaid changed the title Fix CollectionView value type selection Fix CollectionView record struct selection Jan 15, 2026
@hartez
Copy link
Copy Markdown
Contributor

hartez commented Jan 15, 2026

@hartez WDYT?

It took me a bit to figure out what was going on here, but yeah, this change makes sense. Because ListViewBase's SelectedItem is an object, we end up checking for reference equality with == and that's fine for reference types, but record struct is a value so we end up with a copy, and the references don't match.

public record struct TestRecordStruct(string Name);
...
var x = new TestRecordStruct("test");
var y = x;

var ox = x as Object;
var oy = y as Object;

Console.WriteLine($"{ox == oy}"); // False
Console.WriteLine($"{object.Equals(ox, oy)}"); // True

I think it's fine, but I don't work on MAUI anymore, so we should tag in @PureWeen or @mattleibow.

@jeremy-visionaid
Copy link
Copy Markdown
Contributor Author

Ah yup. I just checked the blame and tagged you... Thanks for the review all the same. Much appreciated!

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 11, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33488

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33488"

@MauiBot
Copy link
Copy Markdown
Collaborator

MauiBot commented Mar 22, 2026

🤖 AI Summary

📊 Expand Full Reviewb6ab9da · Merge branch 'dotnet:main' into collectionview-value-type-selection
🔍 Pre-Flight — Context & Validation

Issue: #33487 - CollectionView selection is ineffective with record structs on Windows
PR: #33488 - Fix CollectionView record struct selection
Platforms Affected: Windows (issue); Android requested for testing but not affected by the bug
Files Changed: 1 implementation, 0 test

Key Findings

  • The linked issue is explicitly platform/windows; the reported bug is that CollectionView single selection fails for record struct items on Windows because comparisons end up behaving like reference equality after boxing.
  • The PR changes only src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs, replacing == checks with object.Equals(...) in Windows selection logic.
  • There are no UI, device, or unit test files in the PR, so Gate cannot perform the required FAIL-without-fix / PASS-with-fix verification.
  • PR discussion supports the rationale: boxed record struct values compare unequal by reference with == but equal by value with object.Equals.
  • No prior agent review was found in PR comments or review threads.

Edge Cases From Context

  • The issue is specific to single-selection on Windows; the issue notes multiple-selection already works because it uses value equality.
  • The bug is about value types (record struct) rather than reference types, so the fix should preserve existing behavior for reference-type items.

Fix Candidates

# Source Approach Test Result Files Changed Notes
PR PR #33488 Use object.Equals instead of == when matching SelectedItem in Windows SelectableItemsViewHandler ⏳ PENDING (Gate) src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs Original PR

🚦 Gate — Test Verification

Gate Result: ⚠️ SKIPPED

Result: ❌ SKIPPED

Platform: android
Mode: Full Verification

  • Tests exist: ❌
  • Android affected/applicable: ❌
  • Tests FAIL without fix: ❌
  • Tests PASS with fix: ❌

Notes


🔧 Fix — Analysis & Comparison

Fix Candidates

# Source Approach Test Result Files Changed Notes
1 try-fix (claude-opus-4.6) Centralize selection comparison with ItemEquals(...) and also fix UpdateItemContentControlSelection() ⚠️ Blocked 1 file Found an additional == comparison in visual selection state that the PR does not touch
2 try-fix (claude-sonnet-4.6) Drive single selection by SelectedIndex and compute visual state from ListViewBase.SelectedItems; add a Windows device test ⚠️ Blocked 2 files More invasive but covers both logic and missing test coverage
3 try-fix (gpt-5.3-codex) Minimal extension of the PR: keep current UpdatePlatformSelection() change and also use object.Equals(...) in UpdateItemContentControlSelection() ⚠️ Blocked 1 file Simplest complete alternative identified
4 try-fix (gemini-3-pro-preview) Use EqualityComparer<object>.Default.Equals(...) at both single-selection comparison sites ⚠️ Blocked 1 file Equivalent coverage with a slightly different equality API
5 try-fix (gemini-3-pro-preview, cross-pollination) Explicitly select the exact native item instance via value equality and also fix visual selected-state comparison ⚠️ Blocked 1 file Functionally similar to #3 but more verbose
PR PR #33488 Replace == with object.Equals(...) in UpdatePlatformSelection() only ⚠️ Not runtime-verified 1 file Original PR; does not address the remaining UpdateItemContentControlSelection() comparison

Cross-Pollination

Model Round New Ideas? Details
claude-opus-4.6 2 No NO NEW IDEAS
claude-sonnet-4.6 2 No NO NEW IDEAS
gpt-5.3-codex 2 No NO NEW IDEAS
gemini-3-pro-preview 2 Yes Explicitly select the exact native object instance matching the target value

Exhausted: Yes
Selected Fix: Candidate #3 — smallest change that appears to cover both affected single-selection comparison sites without changing handler flow.


📋 Report — Final Recommendation

⚠️ Final Recommendation: REQUEST CHANGES

Phase Status

Phase Status Notes
Pre-Flight ✅ COMPLETE Windows-only issue; PR changes one Windows handler file and adds no tests
Gate ⚠️ SKIPPED Android was requested but is not an affected platform; no applicable automated test exists
Try-Fix ✅ COMPLETE 5 attempts, 0 runtime-validated due Linux host / Windows-targeting blocker
Report ✅ COMPLETE

Summary

PR #33488 correctly identifies one broken equality site in UpdatePlatformSelection(), but the review found a second single-selection comparison in UpdateItemContentControlSelection() that still uses == and appears subject to the same boxed-value/reference-equality problem. Because the PR also adds no Windows-focused coverage, the current review cannot verify that the change fully fixes #33487 or would catch regressions.

Root Cause

This Windows handler mixes reference equality (==) into single-selection logic for values that may be boxed record structs. Boxed value types compare unequal by reference even when their values are equal. The PR fixes that in native-item matching inside UpdatePlatformSelection(), but the visual selected-state path in UpdateItemContentControlSelection() still compares ItemsView.SelectedItem with FormsDataContext using ==.

Fix Quality

The current PR is directionally correct but likely incomplete. The best alternative found was a minimal extension of the PR: keep the object.Equals(...) changes in UpdatePlatformSelection() and also switch the remaining UpdateItemContentControlSelection() single-selection comparison to value equality. A Windows device test covering record-struct single selection would also be needed so Gate can validate FAIL-without-fix / PASS-with-fix behavior on an affected platform.


@MauiBot MauiBot added s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-gate-failed AI could not verify tests catch the bug s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review) labels Mar 22, 2026
Copy link
Copy Markdown
Contributor

@kubaflo kubaflo left a comment

Choose a reason for hiding this comment

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

Could you please review the AI's summary?

Compare collection items with value equality

Fixes: dotnet#33487
@jeremy-visionaid jeremy-visionaid force-pushed the collectionview-value-type-selection branch from b6ab9da to 03bf678 Compare March 25, 2026 00:14
@jeremy-visionaid
Copy link
Copy Markdown
Contributor Author

Could you please review the AI's summary?

Technically, the AI is correct and we should change it. I don't think that it matters in practice though since reference equality happens to work OK there. However, there is another case that does matter that it missed - in ItemContentControl it needs value equality for the initial selection when the control is realized.

Not sure if you require a test here, or whether you're OK with it just not breaking existing tests?

@jeremy-visionaid jeremy-visionaid changed the title Fix CollectionView record struct selection Fix CollectionView record struct selection on Windows Mar 25, 2026
@kubaflo kubaflo changed the base branch from main to inflight/current March 25, 2026 12:01
@kubaflo kubaflo merged commit 3d81bcd into dotnet:inflight/current Mar 25, 2026
31 checks passed
PureWeen pushed a commit that referenced this pull request Apr 8, 2026
### Description of Change

Compare collection items with value equality

### Issues Fixed

Fixes: #33487

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->
devanathan-vaithiyanathan pushed a commit to devanathan-vaithiyanathan/maui that referenced this pull request Apr 9, 2026
### Description of Change

Compare collection items with value equality

### Issues Fixed

Fixes: dotnet#33487

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-fix-win AI found a better alternative fix than the PR s/agent-gate-failed AI could not verify tests catch the bug s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CollectionView selection is ineffective with record structs on Windows

4 participants