Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix ListView grouping issue on UWP ListView (#14653)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Oct 19, 2021
1 parent e62b6ae commit a717324
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 14095" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue14095">
<StackLayout
Padding="12">
<Label
Text="If group headers are correct, the test has passed."/>
<ListView
x:Name ="lstView"
IsGroupingEnabled="true"
GroupDisplayBinding="{Binding LongName}"
GroupShortNameBinding="{Binding ShortName}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding Name}"
Detail = "{Binding Comment}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.ListView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 14095,
"[Bug] ListView grouping group names not shown on UWP",
PlatformAffected.UWP)]
public partial class Issue14095 : TestContentPage
{
ObservableCollection<GroupedIssue14095Model> Grouped { get; set; }

public Issue14095()
{
#if APP
InitializeComponent();

Grouped = new ObservableCollection<GroupedIssue14095Model> ();
var veggieGroup = new GroupedIssue14095Model () { LongName = "vegetables", ShortName="v" };
var fruitGroup = new GroupedIssue14095Model () { LongName = "fruit", ShortName = "f" };
veggieGroup.Add (new Issue14095Model () { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
veggieGroup.Add (new Issue14095Model () { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
veggieGroup.Add (new Issue14095Model () { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
veggieGroup.Add (new Issue14095Model () { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
fruitGroup.Add (new Issue14095Model () {Name = "banana", IsReallyAVeggie = false,Comment = "available in chip form factor"});
fruitGroup.Add (new Issue14095Model () {Name = "strawberry", IsReallyAVeggie = false,Comment = "spring plant"});
fruitGroup.Add (new Issue14095Model () {Name = "cherry", IsReallyAVeggie = false,Comment = "topper for icecream"});

Grouped.Add (veggieGroup);
Grouped.Add (fruitGroup);

lstView.ItemsSource = Grouped;
#endif
}

protected override void Init()
{
}
}

public class Issue14095Model
{
public string Name { get; set; }
public string Comment { get; set; }
public bool IsReallyAVeggie { get; set; }
public string Image { get; set; }
}

public class GroupedIssue14095Model : ObservableCollection<Issue14095Model>
{
public string LongName { get; set; }
public string ShortName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue13819.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13916.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14095.xaml.cs">
<DependentUpon>Issue14095.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue14523.xaml.cs">
<DependentUpon>Issue14523.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -2864,4 +2867,10 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14095.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
23 changes: 5 additions & 18 deletions Xamarin.Forms.Platform.UAP/CellControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,10 @@ void SetCell(object newContext)
// If there is a ListView, load the Cell content from the ItemTemplate.
// Otherwise, the given Cell is already a templated Cell from a TableView.
ListView lv = _listView.Value;

if (lv != null)
{
// 🚀 If there is an old cell, check if it was a group header
// we need this later to know whether we can recycle this cell
bool? wasGroupHeader = null;
var oldCell = Cell;
if (oldCell != null)
{
wasGroupHeader = oldCell.GetIsGroupHeader<ItemsView<Cell>, Cell>();
}

Cell oldCell = Cell;
bool isGroupHeader = IsGroupHeader;
DataTemplate template = isGroupHeader ? lv.GroupHeaderTemplate : lv.ItemTemplate;
object bindingContext = newContext;
Expand All @@ -358,15 +351,8 @@ void SetCell(object newContext)
sameTemplate = oldTemplate == template;
}
}
// 🚀 if there is no datatemplateselector, we now verify if the old cell
// was a groupheader and whether the new one is as well.
// Again, this is only to verify we can reuse this cell
else if (wasGroupHeader.HasValue)
{
sameTemplate = wasGroupHeader == isGroupHeader;
}

// reuse cell
// Reuse cell
var canReuseCell = Cell != null && sameTemplate;

// 🚀 If we can reuse the cell, just reuse it...
Expand Down Expand Up @@ -403,7 +389,8 @@ void SetCell(object newContext)

if (Cell != cell)
Cell = cell;
// 🚀 even if the cell did not change, we **must** call SendDisappearing() and SendAppearing()

// 🚀 Even if the cell did not change, we **must** call SendDisappearing() and SendAppearing()
// because frameworks such as Reactive UI rely on this! (this.WhenActivated())
else if (Cell != null)
{
Expand Down

0 comments on commit a717324

Please sign in to comment.