Skip to content

Commit e9d69ec

Browse files
[Windows] Fix CollectionView selected item visibility (#31126)
* Fixed CollectionView multiselection. * updated StructuredItemsViewHandler.Windows.cs * added UI test. * added snapshots. * Update StructuredItemsViewHandler.Windows.cs * updated review concerns. * added snapshots. * Updated test * removed existing output images. * snapshots added. * Update Issue30868.cs * Update Issue30868.cs * added windows snapshot.
1 parent 17f4baf commit e9d69ec

17 files changed

+116
-0
lines changed

src/Controls/src/Core/Handlers/Items/StructuredItemsViewHandler.Windows.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public partial class StructuredItemsViewHandler<TItemsView> : ItemsViewHandler<T
1818
View _currentFooter;
1919
WeakNotifyPropertyChangedProxy _layoutPropertyChangedProxy;
2020
PropertyChangedEventHandler _layoutPropertyChanged;
21+
const string ListViewItemStyleKey = "DefaultListViewItemStyle";
22+
const string GridViewItemStyleKey = "DefaultGridViewItemStyle";
23+
static WStyle _listViewItemStyle;
24+
static WStyle _gridViewItemStyle;
2125

2226
~StructuredItemsViewHandler() => _layoutPropertyChangedProxy?.Unsubscribe();
2327

@@ -82,6 +86,9 @@ public static void MapItemSizingStrategy(StructuredItemsViewHandler<TItemsView>
8286

8387
protected override ListViewBase SelectListViewBase()
8488
{
89+
_listViewItemStyle = GetDefaultStyle(ListViewItemStyleKey);
90+
_gridViewItemStyle = GetDefaultStyle(GridViewItemStyleKey);
91+
8592
switch (VirtualView.ItemsLayout)
8693
{
8794
case GridItemsLayout gridItemsLayout:
@@ -245,20 +252,35 @@ static WStyle GetItemContainerStyle(GridItemsLayout layout)
245252

246253
var style = new WStyle(typeof(GridViewItem));
247254

255+
if (_gridViewItemStyle is not null)
256+
{
257+
style.BasedOn = _gridViewItemStyle;
258+
}
259+
248260
style.Setters.Add(new WSetter(FrameworkElement.MarginProperty, margin));
249261
style.Setters.Add(new WSetter(Control.PaddingProperty, WinUIHelpers.CreateThickness(0)));
250262
style.Setters.Add(new WSetter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));
251263

252264
return style;
253265
}
254266

267+
static WStyle GetDefaultStyle(string resourceKey)
268+
{
269+
return Microsoft.UI.Xaml.Application.Current.Resources[resourceKey] as WStyle;
270+
}
271+
255272
static WStyle GetVerticalItemContainerStyle(LinearItemsLayout layout)
256273
{
257274
var v = layout?.ItemSpacing ?? 0;
258275
var margin = WinUIHelpers.CreateThickness(0, v, 0, v);
259276

260277
var style = new WStyle(typeof(ListViewItem));
261278

279+
if (_listViewItemStyle is not null)
280+
{
281+
style.BasedOn = _listViewItemStyle;
282+
}
283+
262284
style.Setters.Add(new WSetter(FrameworkElement.MinHeightProperty, 0));
263285
style.Setters.Add(new WSetter(FrameworkElement.MarginProperty, margin));
264286
style.Setters.Add(new WSetter(Control.PaddingProperty, WinUIHelpers.CreateThickness(0)));
@@ -274,6 +296,11 @@ static WStyle GetHorizontalItemContainerStyle(LinearItemsLayout layout)
274296

275297
var style = new WStyle(typeof(ListViewItem));
276298

299+
if (_listViewItemStyle is not null)
300+
{
301+
style.BasedOn = _listViewItemStyle;
302+
}
303+
277304
style.Setters.Add(new WSetter(FrameworkElement.MinWidthProperty, 0));
278305
style.Setters.Add(new WSetter(Control.PaddingProperty, padding));
279306
style.Setters.Add(new WSetter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Stretch));
17.3 KB
Loading
17.3 KB
Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
5+
[Issue(IssueTracker.Github, 30868, "CollectionView selection visual states", PlatformAffected.UWP)]
6+
public class Issue30868 : ContentPage
7+
{
8+
public ObservableCollection<string> Items { get; set; }
9+
10+
public Issue30868()
11+
{
12+
Items = new ObservableCollection<string>
13+
{
14+
"Item 1",
15+
"Item 2",
16+
"Item 3"
17+
};
18+
19+
var collectionView = new CollectionView
20+
{
21+
ItemsSource = Items,
22+
SelectionMode = SelectionMode.Multiple,
23+
HeightRequest = 40,
24+
AutomationId = "collectionViewSelectionMode",
25+
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal)
26+
{
27+
ItemSpacing = 4
28+
},
29+
ItemTemplate = new DataTemplate(() =>
30+
{
31+
var label = new Label
32+
{
33+
FontSize = 16,
34+
VerticalOptions = LayoutOptions.Center,
35+
VerticalTextAlignment = TextAlignment.Center,
36+
Padding = new Thickness(10, 0, 10, 10),
37+
TextColor = Colors.Purple
38+
};
39+
label.SetBinding(Label.TextProperty, ".");
40+
return label;
41+
})
42+
};
43+
44+
Content = collectionView;
45+
}
46+
}
7.58 KB
Loading
7.58 KB
Loading
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue30868 : _IssuesUITest
8+
{
9+
public Issue30868(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
public override string Issue => "CollectionView selection visual states";
13+
14+
#if TEST_FAILS_ON_WINDOWS // Using AppThemeBinding and changing theme not working on Windows
15+
[Test, Order(1)]
16+
[Category(UITestCategories.CollectionView)]
17+
public void CollectionViewSelectionModeOnDarkTheme()
18+
{
19+
try
20+
{
21+
App.SetDarkTheme();
22+
App.WaitForElement("Item 2");
23+
App.Tap("Item 2");
24+
VerifyScreenshot();
25+
}
26+
finally
27+
{
28+
App.SetLightTheme();
29+
}
30+
}
31+
#endif
32+
33+
[Test, Order(2)]
34+
[Category(UITestCategories.CollectionView)]
35+
public void CollectionViewSelectionModeOnLightTheme()
36+
{
37+
App.WaitForElement("Item 2");
38+
#if WINDOWS
39+
App.Tap("Item 2");
40+
#endif
41+
VerifyScreenshot();
42+
}
43+
}
1.59 KB
Loading
7.4 KB
Loading
1.8 KB
Loading

0 commit comments

Comments
 (0)