Skip to content

Commit

Permalink
Pickers Inside CollectionView get SelectedItem Cleared on Scrolling -…
Browse files Browse the repository at this point in the history
… fix
  • Loading branch information
kubaflo committed Dec 23, 2024
1 parent dde0d73 commit 8cf55f0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Controls/src/Core/Picker/Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ void OnItemsSourceChanged(IList oldValue, IList newValue)
}
else
{
// Unlock, then clear, so OnItemsCollectionChanged executes
((LockableObservableListWrapper)Items).IsLocked = false;
((LockableObservableListWrapper)Items).InternalClear();
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25842.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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.Issue25842">
<ContentView>
<CollectionView HeightRequest="600"
AutomationId="CollectionView"
VerticalOptions="Center"
ItemsSource="{Binding Students}">
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
Span="1"
HorizontalItemSpacing="10"
VerticalItemSpacing="10"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border Stroke="Black"
StrokeThickness="1"
Padding="10"
BackgroundColor="Gray"
StrokeShape="RoundRectangle 10">
<VerticalStackLayout Spacing="10">
<HorizontalStackLayout VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
Spacing="10">
<Label Text="Name:"
FontAttributes="Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"/>
<Label Text="{Binding Name}"
FontSize="Medium"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"/>
</HorizontalStackLayout>

<HorizontalStackLayout VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
Spacing="10">
<Label Text="Country:"
FontAttributes="Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"/>
<Picker Title="Country"
WidthRequest="300"
ItemsSource="{Binding Countries}"
SelectedItem="{Binding Country}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"
FontSize="Medium"/>
</HorizontalStackLayout>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentView>
</ContentPage>
42 changes: 42 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25842.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 25842, "Pickers Inside CollectionView get SelectedItem Cleared on Scrolling", PlatformAffected.iOS)]
public partial class Issue25842 : ContentPage
{
public Issue25842()
{
InitializeComponent();
BindingContext = new Issue25842ViewModel();
}

class Issue25842ViewModel
{
public ObservableCollection<Student> Students { get; set; }

public Issue25842ViewModel()
{
Students = new()
{
new () { Name = "John Doe", Country = "United States" },
new () { Name = "Jane Smith", Country = "Canada" },
new () { Name = "Sam Brown", Country = "India" },
new () { Name = "Lisa White", Country = "United States" },
new () { Name = "Tom Green", Country = "Canada" },
new () { Name = "Emma Black", Country = "India" },
new () { Name = "Noah Blue", Country = "United States" },
new () { Name = "Olivia Yellow", Country = "Canada" },
new () { Name = "Liam Brown", Country = "India" },
new () { Name = "Sophia Pink", Country = "United States" }
};
}

public class Student
{
public required string Name { get; set; }
public required string Country { get; set; }
public ObservableCollection<string> Countries { get; set; } = new() { "United States", "Canada", "India" };
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue25842 : _IssuesUITest
{
public Issue25842(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Pickers Inside CollectionView get SelectedItem Cleared on Scrolling";

[Test]
[Description("Verify that OnTapped is fired every time a ViewCell is tapped")]
[Category(UITestCategories.CollectionView)]
[Category(UITestCategories.Picker)]
public void SelectedPickerItemsShouldNotClear()
{
App.WaitForElement("CollectionView");
App.ScrollDown("CollectionView");
App.ScrollUp("CollectionView");
VerifyScreenshot();
}
}
}

0 comments on commit 8cf55f0

Please sign in to comment.