diff --git a/src/Controls/src/Core/Picker/Picker.cs b/src/Controls/src/Core/Picker/Picker.cs index 399235196aa4..45f60e651720 100644 --- a/src/Controls/src/Core/Picker/Picker.cs +++ b/src/Controls/src/Core/Picker/Picker.cs @@ -238,6 +238,9 @@ string GetDisplayMember(object item) static object CoerceSelectedIndex(BindableObject bindable, object value) { var picker = (Picker)bindable; + if (picker.Items is not null && !picker._itemsLoaded) + return value; + return picker.Items == null ? -1 : ((int)value).Clamp(-1, picker.Items.Count - 1); } @@ -351,6 +354,7 @@ void ResetItems() ((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item)); Handler?.UpdateValue(nameof(IPicker.Items)); + _itemsLoaded = true; ClampSelectedIndex(); } @@ -434,6 +438,8 @@ void ITextAlignmentElement.OnHorizontalTextAlignmentPropertyChanged(TextAlignmen IList IPicker.Items => Items; + bool _itemsLoaded; + int IPicker.SelectedIndex { get => SelectedIndex; diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/SelectedIndexShouldBeRespectedOnPageLoad.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/SelectedIndexShouldBeRespectedOnPageLoad.png new file mode 100644 index 000000000000..ab61164aa954 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/SelectedIndexShouldBeRespectedOnPageLoad.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml new file mode 100644 index 000000000000..ee7b203d7a33 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml @@ -0,0 +1,19 @@ + + + + + + + 0 + 1 + 2 + + + + + \ No newline at end of file diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml.cs new file mode 100644 index 000000000000..79de5ee8d872 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml.cs @@ -0,0 +1,15 @@ +using Microsoft.Maui.Controls; +using Microsoft.Maui.Controls.Xaml; + +namespace Maui.Controls.Sample.Issues +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + [Issue(IssueTracker.Github, 9150, "Picker Attribute \"SelectedIndex\" Not being respected on page load", PlatformAffected.All)] + public partial class Issue9150 : ContentPage + { + public Issue9150() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue9150.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue9150.cs new file mode 100644 index 000000000000..a4b2d860d70a --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue9150.cs @@ -0,0 +1,25 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues +{ + public class Issue9150 : _IssuesUITest + { + public Issue9150(TestDevice device) : base(device) + { + } + + public override string Issue => "Picker Attribute \"SelectedIndex\" Not being respected on page load"; + + [Test] + [Category(UITestCategories.Picker)] + public void SelectedIndexShouldBeRespectedOnPageLoad() + { + App.WaitForElement("picker"); + + //Selected item should be equal to '1' + VerifyScreenshot(); + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/SelectedIndexShouldBeRespectedOnPageLoad.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/SelectedIndexShouldBeRespectedOnPageLoad.png new file mode 100644 index 000000000000..dc0a743a0065 Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/SelectedIndexShouldBeRespectedOnPageLoad.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/SelectedIndexShouldBeRespectedOnPageLoad.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/SelectedIndexShouldBeRespectedOnPageLoad.png new file mode 100644 index 000000000000..ed634987a9de Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/SelectedIndexShouldBeRespectedOnPageLoad.png differ