-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fixed issue where group Header/Footer template was applied to all items when IsGrouped was true for an ObservableCollection #28886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0aa7056
28b7eca
f79119c
0daa130
8d33f1f
41a91ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| 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> |
| 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 | ||||||
|
||||||
| #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. |
There was a problem hiding this comment.
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 inObservableGroupedSource.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
INotifyCollectionChangedcheck here as well:This would ensure that only items implementing both interfaces are treated as observable groups, matching the iOS behavior.