Skip to content

[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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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);
}
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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>
214 changes: 214 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,214 @@
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,
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,
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
// 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");

VerifyScreenshot();
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.