Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -163,9 +163,16 @@ bool PositionIsSelected(int position)
void SelectableClicked(object sender, int adapterPosition)
{
if (adapterPosition >= 0 && adapterPosition < ItemsSource?.Count)
{
UpdateMauiSelection(adapterPosition);
}
{
UpdateMauiSelection(adapterPosition);
// Unconditionally sync visual state for Single mode.
// Handles value-equal items where PropertyChanged is suppressed.
if (ItemsView.SelectionMode == SelectionMode.Single && sender is SelectableViewHolder clickedHolder)
{
ClearPlatformSelection();
clickedHolder.IsSelected = true;
}
}
}

void UpdateMauiSelection(int adapterPosition)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue20062.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Microsoft.Maui.Controls.Shapes;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 20062, "CollectionView - SelectedItem visual state manager not working",
PlatformAffected.Android)]
public class Issue20062 : TestContentPage
{
CollectionView _collectionView;
List<string> list = new List<string>();

protected override void Init()
{

_collectionView = new CollectionView
{
SelectionMode = SelectionMode.Single,
ItemsLayout = new GridItemsLayout(2, ItemsLayoutOrientation.Vertical)
{
HorizontalItemSpacing = 12,
VerticalItemSpacing = 12
},
ItemTemplate = new DataTemplate(() =>
{
var border = new Border
{
Padding = 4,
BackgroundColor = Colors.White,
Stroke = Colors.Gray,
StrokeShape = new RoundRectangle { CornerRadius = 12 },
StrokeThickness = 1
};

var label = new Label
{
FontFamily = "SemiBold",
TextColor = Colors.Black,
Text = "a",
FontSize = 14,
HorizontalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.Center
};
var grid = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = new GridLength(80) },
new RowDefinition { Height = new GridLength(40) }
}
};

grid.Add(label, 0, 1);

border.Content = grid;
VisualStateManager.SetVisualStateGroups(border, new VisualStateGroupList
{
new VisualStateGroup
{
Name = "CommonStates",
States =
{
new VisualState { Name = "Normal" },
new VisualState
{
Name = "Selected",
Setters =
{
new Setter { Property = Border.BackgroundColorProperty, Value = Colors.Blue },
new Setter { Property = Border.StrokeProperty, Value = Colors.Red }
}
}
}
}
});

return border;
})
};
_collectionView.AutomationId = "CollectionView";

var layout = new VerticalStackLayout
{
Padding = new Thickness(30, 0),
Spacing = 25,
Children = { _collectionView }
};
list.Add("a");
list.Add("a");
list.Add("a");
list.Add("a");
_collectionView.ItemsSource = list;

Content = new ScrollView
{
Content = layout
};

}
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if TEST_FAILS_ON_WINDOWS // When selecting a single item, the selection background is applied to all items.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue20062 : _IssuesUITest
{
public Issue20062(TestDevice device) : base(device) { }

public override string Issue => "CollectionView - SelectedItem visual state manager not working";

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewSelectionChangesVisualState()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test failing on iOS:
image
Could you review it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test failing on iOS: image Could you review it?

I have updated the snapshot for iOS platform.

{
App.WaitForElement("CollectionView");
var items = App.FindElementsByText("a");
items.ElementAt(0).Tap();
items.ElementAt(2).Tap();
VerifyScreenshot();
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading