KEMBAR78
Fixed CollectionView does not update layout correctly when ItemsSource changes by Dhivya-SF4094 · Pull Request #30978 · dotnet/maui · GitHub
Skip to content

Conversation

Dhivya-SF4094
Copy link
Contributor

@Dhivya-SF4094 Dhivya-SF4094 commented Aug 1, 2025

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

The CollectionView is initially loaded with its ItemsSource set to null. When the ItemsSource is updated at runtime, the items are not displayed, and the CollectionView height remains at 0, resulting in no visible content.

Root Cause

  • When the CollectionView is initially arranged with no items (i.e., ItemsSource is null), the layout system measures it with zero height.
  • Later, when the ItemsSource is updated at runtime, the CollectionView attempts to re-measure, but in CollectionViewHandler2, the GetDesiredSize method still returns a height of 0 because Controller.GetSize() is called before any cells have been realized or measured. Since the layout system continues to receive a zero height, it does not allocate space for the CollectionView, and as a result, the cells are never created or displayed.

Description of Change

Added a fallback in GetDesiredSize to return base.GetDesiredSize() when Controller.GetSize() returns a zero width or height. This ensures proper layout measurement and allows the CollectionView to render correctly once data is set.

Reference
if (potentialContentSize == null)
{
return size;
}

Validated the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed:

Fixes #30953

Screenshots

Before  After 
 
30953_BeforeFix.mov
  
30953_AfterFix.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Aug 1, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@Dhivya-SF4094! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Aug 1, 2025
@sheiksyedm sheiksyedm added platform/ios area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-cv2 labels Aug 1, 2025
@Dhivya-SF4094 Dhivya-SF4094 marked this pull request as ready for review August 1, 2025 15:07
@Copilot Copilot AI review requested due to automatic review settings August 1, 2025 15:07
@Dhivya-SF4094 Dhivya-SF4094 requested a review from a team as a code owner August 1, 2025 15:07
Copy link
Contributor

@Copilot 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 a layout issue where CollectionView fails to display items when ItemsSource is set at runtime after being initially null. The problem occurs because the layout system measures the CollectionView with zero height when no items are present, and continues to receive zero height measurements even after items are added.

Key changes:

  • Added fallback logic in GetDesiredSize() method to use base implementation when controller returns zero dimensions
  • Added comprehensive UI test to validate the fix across platforms

Reviewed Changes

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

File Description
src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs Added fallback to base.GetDesiredSize() when Controller.GetSize() returns zero width/height
src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs Created test page demonstrating CollectionView with runtime ItemsSource assignment
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30953.cs Added automated UI test to verify CollectionView displays items after runtime ItemsSource change
Comments suppressed due to low confidence (2)

src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs:8

  • The field name 'Countries' should follow C# naming conventions and be lowercase 'countries' since it's a private field.
	private ObservableCollection<string> Countries = new();

src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs:13

  • The CollectionView2 instance lacks an AutomationId. According to the coding guidelines, AutomationIds should be unique and present on all testable UI elements to ensure WaitForElement can locate the correct element in automated tests.
		var collectionView = new CollectionView2();

@rmarinho
Copy link
Member

rmarinho commented Aug 4, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen changed the base branch from main to inflight/current August 4, 2025 19:58
@PureWeen PureWeen merged commit a4ef877 into dotnet:inflight/current Aug 4, 2025
121 of 129 checks passed
PureWeen pushed a commit that referenced this pull request Aug 4, 2025
…e changes (#30978)

* Fixed CollectionView does not update layout correctly when ItemsSource changes

* Optimized fix
github-actions bot pushed a commit that referenced this pull request Aug 7, 2025
…e changes (#30978)

* Fixed CollectionView does not update layout correctly when ItemsSource changes

* Optimized fix
PureWeen added a commit that referenced this pull request Aug 8, 2025
---
title: ".NET MAUI main-to-inflight-candidate Release Notes"
date: 2025-08-08
version: "main-to-inflight-candidate"
comparison: "main...inflight/candidate"
full_changelog:
"main...inflight/candidate"
description: "Release notes for .NET MAUI main-to-inflight-candidate,
covering 10 commits with enhancements and improvements."
---

# .NET MAUI main-to-inflight-candidate Release Notes

**Version:** main-to-inflight-candidate  
**Comparison:** main...inflight/candidate  
**Generated:** August 08, 2025  
**Last Updated:** August 08, 2025  
**Latest Commit Range:** 7f99494  
**Total Commits:** 10

## What's Changed

This release includes 10 commits with various improvements, bug fixes,
and enhancements to the .NET MAUI framework.


### .NET MAUI Product Fixes
- Update MauiRecyclerView.cs by @github-actions[bot] in
#31050
- Added the GraphicsView base images for Windows and mac platforms by
@nivetha-nagalingam in #31018
- Fixed CollectionView does not update layout correctly when ItemsSource
changes by @Dhivya-SF4094 in #30978 -
Fixes #30953
- Fix duplicate app icon resizing by @jeremy-visionaid in
#30920
- Fixed GraphicsView event handlers are triggered even when IsEnabled is
set to False by @Dhivya-SF4094 in
#30755 - Fixes
#30649
- [iOS, Mac] Fix for Picker title is not displayed again when
SelectedIndex is set to -1 by @HarishwaranVijayakumar in
#30486 - Fixes
#31011,
#30463

### Testing
- [Testing] Feature matrix UITest Cases for GraphicsView Control by
@nivetha-nagalingam in #30781
- [Testing] Feature matrix UITest Cases for HybridWebView Control by
@NafeelaNazhir in #30613

### Housekeeping
- [create-pull-request] automated change by @github-actions[bot] in
#30890
- [create-pull-request] automated change by @github-actions[bot] in
#30700

**Full Changelog**:
main...inflight/candidate
SuthiYuvaraj pushed a commit to SuthiYuvaraj/maui that referenced this pull request Aug 12, 2025
…e changes (dotnet#30978)

* Fixed CollectionView does not update layout correctly when ItemsSource changes

* Optimized fix
rmarinho pushed a commit that referenced this pull request Aug 13, 2025
…e changes (#30978)

* Fixed CollectionView does not update layout correctly when ItemsSource changes

* Optimized fix
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-cv2 community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CollectionView does not update layout correctly when ItemsSource changes

4 participants