From a3061aad3db92020677624defdb5fc812b819f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Fri, 24 Sep 2021 12:40:34 +0200 Subject: [PATCH] Fix ListView grouping issue on UWP ListView --- .../Issue14095.xaml | 28 ++++++++ .../Issue14095.xaml.cs | 65 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 9 +++ Xamarin.Forms.Platform.UAP/CellControl.cs | 23 ++----- 4 files changed, 107 insertions(+), 18 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml new file mode 100644 index 00000000000..b995775efea --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml @@ -0,0 +1,28 @@ +ο»Ώ + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml.cs new file mode 100644 index 00000000000..c69261af4a1 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue14095.xaml.cs @@ -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 Grouped { get; set; } + + public Issue14095() + { +#if APP + InitializeComponent(); + + Grouped = new ObservableCollection (); + 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 + { + public string LongName { get; set; } + public string ShortName { get; set; } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 62258dae750..5eac71f6182 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -23,6 +23,9 @@ + + Issue14095.xaml + Issue14523.xaml @@ -2800,4 +2803,10 @@ MSBuild:UpdateDesignTimeXaml + + + Designer + MSBuild:Compile + + \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/CellControl.cs b/Xamarin.Forms.Platform.UAP/CellControl.cs index dca2b02555d..ab3743bb4dd 100644 --- a/Xamarin.Forms.Platform.UAP/CellControl.cs +++ b/Xamarin.Forms.Platform.UAP/CellControl.cs @@ -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, Cell>(); - } - + Cell oldCell = Cell; bool isGroupHeader = IsGroupHeader; DataTemplate template = isGroupHeader ? lv.GroupHeaderTemplate : lv.ItemTemplate; object bindingContext = newContext; @@ -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... @@ -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) {