Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ void UpdateGroupTracking()

for (int n = 0; n < _groupSource.Count; n++)
{
var source = ItemsSourceFactory.Create(_groupSource[n] as IEnumerable, _groupableItemsView, this);
source.HasFooter = _hasGroupFooters;
source.HasHeader = _hasGroupHeaders;
_groups.Add(source);
if (_groupSource[n] is IEnumerable list)
{
var source = ItemsSourceFactory.Create(list, _groupableItemsView, this);
source.HasFooter = _hasGroupFooters;
source.HasHeader = _hasGroupHeaders;
_groups.Add(source);
}
Comment on lines +229 to +235
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

The fix only checks if the item is IEnumerable, but the iOS implementation in ObservableGroupedSource.cs (line 136) has a more restrictive check: if (_groupSource[n] is INotifyCollectionChanged && _groupSource[n] is IEnumerable list).

For consistency across platforms and to ensure proper observable behavior, consider adding the INotifyCollectionChanged check here as well:

if (_groupSource[n] is INotifyCollectionChanged && _groupSource[n] is IEnumerable list)
{
    var source = ItemsSourceFactory.Create(list, _groupableItemsView, this);
    source.HasFooter = _hasGroupFooters;
    source.HasHeader = _hasGroupHeaders;
    _groups.Add(source);
}

This would ensure that only items implementing both interfaces are treated as observable groups, matching the iOS behavior.

Copilot uses AI. Check for mistakes.
Comment on lines +229 to +235
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

This PR fixes the DEPRECATED Items/ handlers (located at src/Controls/src/Core/Handlers/Items/), not the current Items2/ handlers.

According to the coding guidelines (CodingGuidelineID: 1000002), the Items/ handlers are deprecated and Items2/ is the active implementation. However, since Items2/ only has iOS implementation and Android CollectionView still uses Items/, this fix is appropriate for the current codebase.

Note: When Items2/ Android support is added in the future, this fix will need to be implemented there as well.

Copilot generated this review using guidance from repository custom instructions.
}
}

Expand Down
81 changes: 81 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28827.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue28827"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">

<Grid Padding="10"
RowDefinitions="*, *">
<CollectionView
Grid.Row="0"
x:Name="collectionView"
x:DataType="ns:Issue28827CollectionViewViewModel"
ItemsSource="{Binding ItemsSource}"
ItemTemplate="{Binding ItemTemplate}"
IsGrouped="{Binding IsGrouped}"
GroupHeaderTemplate="{Binding GroupHeaderTemplate}"
GroupFooterTemplate="{Binding GroupFooterTemplate}"
AutomationId="collectionView">
</CollectionView>

<ScrollView Grid.Row="1">
<StackLayout Padding="10"
Spacing="10">
<Label Text="GroupHeaderTemplate:"
FontAttributes="Bold"
FontSize="12"/>
<StackLayout Orientation="Horizontal">
<RadioButton x:Name="GroupHeaderTemplateNone"
IsChecked="True"
CheckedChanged="OnGroupHeaderTemplateChanged"
Content="None"
FontSize="11"
GroupName="GroupHeaderTemplateGroup"
AutomationId="GroupHeaderTemplateNone"/>
<RadioButton x:Name="GroupHeaderTemplateGrid"
CheckedChanged="OnGroupHeaderTemplateChanged"
Content="View"
FontSize="11"
GroupName="GroupHeaderTemplateGroup"
AutomationId="GroupHeaderTemplateGrid"/>
</StackLayout>

<Label Text="GroupFooterTemplate:"
FontAttributes="Bold"
FontSize="12"/>
<StackLayout Orientation="Horizontal">
<RadioButton x:Name="GroupFooterTemplateNone"
IsChecked="True"
CheckedChanged="OnGroupFooterTemplateChanged"
Content="None"
FontSize="11"
GroupName="GroupFooterTemplateGroup"
AutomationId="GroupFooterTemplateNone"/>
<RadioButton x:Name="GroupFooterTemplateGrid"
CheckedChanged="OnGroupFooterTemplateChanged"
Content="View"
FontSize="11"
GroupName="GroupFooterTemplateGroup"
AutomationId="GroupFooterTemplateGrid"/>
</StackLayout>

<Label Text="IsGrouped:"
FontSize="12"
FontAttributes="Bold"/>
<StackLayout Orientation="Horizontal">
<RadioButton x:Name="IsGroupedFalse"
Content="False"
IsChecked="True"
CheckedChanged="OnIsGroupedChanged"
FontSize="11"
AutomationId="IsGroupedFalse"/>
<RadioButton x:Name="IsGroupedTrue"
Content="True"
CheckedChanged="OnIsGroupedChanged"
FontSize="11"
AutomationId="IsGroupedTrue"/>
</StackLayout>
</StackLayout>
</ScrollView>
</Grid>
</ContentPage>
216 changes: 216 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28827.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 28827, "[Android] Group Header/Footer set for all Items when IsGrouped is True for ObservableCollection", PlatformAffected.Android)]
public partial class Issue28827 : ContentPage
{
Issue28827CollectionViewViewModel _viewModel;
public Issue28827()
{
InitializeComponent();
BindingContext = _viewModel = new Issue28827CollectionViewViewModel();
}

void OnGroupHeaderTemplateChanged(object sender, CheckedChangedEventArgs e)
{
if (GroupHeaderTemplateNone.IsChecked)
{
_viewModel.GroupHeaderTemplate = null;
}
else if (GroupHeaderTemplateGrid.IsChecked)
{
_viewModel.GroupHeaderTemplate = new DataTemplate(() =>
{
return new Grid
{
BackgroundColor = Colors.LightGray,
Padding = new Thickness(10),
Children =
{
new Label
{
Text = "Group Header Template (Grid View)",
FontSize = 18,
AutomationId = "GroupHeaderTemplate",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Colors.Green
}
}
};
});
}
}

void OnGroupFooterTemplateChanged(object sender, CheckedChangedEventArgs e)
{
if (GroupFooterTemplateNone.IsChecked)
{
_viewModel.GroupFooterTemplate = null;
}
else if (GroupFooterTemplateGrid.IsChecked)
{
_viewModel.GroupFooterTemplate = new DataTemplate(() =>
{
return new Grid
{
BackgroundColor = Colors.LightGray,
Padding = new Thickness(10),
Children =
{
new Label
{
Text = "Group Footer Template (Grid View)",
FontSize = 18,
AutomationId = "GroupFooterTemplate",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Colors.Red
}
}
};
});
}
}

void OnIsGroupedChanged(object sender, CheckedChangedEventArgs e)
{
if (IsGroupedFalse.IsChecked)
{
_viewModel.IsGrouped = false;
}
else if (IsGroupedTrue.IsChecked)
{
_viewModel.IsGrouped = true;
}
}
}

public class Issue28827CollectionViewViewModel : INotifyPropertyChanged
{
DataTemplate _groupHeaderTemplate;
DataTemplate _groupFooterTemplate;
DataTemplate _itemTemplate;
bool _isGrouped = false;
ObservableCollection<Issue28827ItemModel> _observableCollection;

public event PropertyChangedEventHandler PropertyChanged;

public Issue28827CollectionViewViewModel()
{
LoadItems();
ItemTemplate = new DataTemplate(() =>
{
var stackLayout = new StackLayout
{
Padding = new Thickness(10),
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};

var label = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
label.SetBinding(Label.TextProperty, "Caption");
stackLayout.Children.Add(label);
return stackLayout;
});

GroupHeaderTemplate = new DataTemplate(() =>
{
var stackLayout = new StackLayout
{
BackgroundColor = Colors.LightGray
};
var label = new Label
{
FontAttributes = FontAttributes.Bold,
FontSize = 18
};
label.SetBinding(Label.TextProperty, "Key");
stackLayout.Children.Add(label);
return stackLayout;
});
}

void LoadItems()
{
_observableCollection = new ObservableCollection<Issue28827ItemModel>
{
new Issue28827ItemModel { Caption = "Item 1" },
new Issue28827ItemModel { Caption = "Item 2" },
new Issue28827ItemModel { Caption = "Item 3" }
};
}

public DataTemplate GroupHeaderTemplate
{
get => _groupHeaderTemplate;
set { _groupHeaderTemplate = value; OnPropertyChanged(); }
}

public DataTemplate GroupFooterTemplate
{
get => _groupFooterTemplate;
set { _groupFooterTemplate = value; OnPropertyChanged(); }
}

public DataTemplate ItemTemplate
{
get => _itemTemplate;
set { _itemTemplate = value; OnPropertyChanged(); }
}

public bool IsGrouped
{
get => _isGrouped;
set { _isGrouped = value; OnPropertyChanged(); }
}

public object ItemsSource
{
get => _observableCollection;
}

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (propertyName == nameof(IsGrouped))
{
OnPropertyChanged(nameof(ItemsSource));
}

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

internal class Issue28827Grouping<TKey, TItem> : List<TItem>
{
public TKey Key { get; }

public Issue28827Grouping(TKey key, List<TItem> items) : base(items)
{
Key = key;
}

public override string ToString()
{
return Key?.ToString() ?? base.ToString();
}
}

internal class Issue28827ItemModel
{
public string Caption { get; set; }

public override string ToString()
{
return !string.IsNullOrEmpty(Caption) ? Caption : base.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if TEST_FAILS_ON_WINDOWS // NullReferenceException occurs when switching isGrouped to true
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

[nitpick] The test is excluded from Windows using #if TEST_FAILS_ON_WINDOWS with a reference to issue #28824, which indicates there's a separate NullReferenceException bug on Windows.

However, according to the PR description, the fix was tested on Windows (MacCatalyst checkbox is checked, which runs on macOS). The compilation directive name suggests the test itself fails on Windows, not that the fix doesn't work there.

Consider clarifying:

  1. Does the underlying bug (issue [Android] Group Header/Footer Repeated for All Items When IsGrouped is True for ObservableCollection #28827) affect Windows, or only the test infrastructure?
  2. If Windows has the same grouped collection bug, should a Windows-specific version of this test be created once issue [Windows] NullReferenceException thrown When Toggling IsGrouped to True in ObservableCollection Binding #28824 is resolved?
  3. Should the comment explain why Windows is excluded more clearly (e.g., "Test excluded on Windows due to unrelated NullReferenceException in test infrastructure - see [Windows] NullReferenceException thrown When Toggling IsGrouped to True in ObservableCollection Binding #28824")?
Suggested change
#if TEST_FAILS_ON_WINDOWS // NullReferenceException occurs when switching isGrouped to true
#if TEST_FAILS_ON_WINDOWS // Test excluded on Windows due to unrelated NullReferenceException in test infrastructure - see https://github.com/dotnet/maui/issues/28824. The underlying grouped collection bug (issue #28827) is not Windows-specific; re-enable this test on Windows once issue #28824 is resolved.

Copilot uses AI. Check for mistakes.
// refer to https://github.com/dotnet/maui/issues/28824
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue28827 : _IssuesUITest
{
public override string Issue => "[Android] Group Header/Footer set for all Items when IsGrouped is True for ObservableCollection";
public Issue28827(TestDevice device)
: base(device)
{ }

[Test]
[Category(UITestCategories.CollectionView)]
public void CVGroupHFTemplateWithObservableCollection()
{
App.WaitForElement("collectionView");
App.Tap("IsGroupedTrue");
App.Tap("GroupHeaderTemplateGrid");
App.Tap("GroupFooterTemplateGrid");
App.WaitForNoElement("GroupHeaderTemplate");
App.WaitForNoElement("GroupFooterTemplate");
}
}
#endif
Loading