Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/Controls/src/Core/Picker/Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -351,6 +354,7 @@ void ResetItems()
((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item));
Handler?.UpdateValue(nameof(IPicker.Items));

_itemsLoaded = true;
ClampSelectedIndex();
}

Expand Down Expand Up @@ -434,6 +438,8 @@ void ITextAlignmentElement.OnHorizontalTextAlignmentPropertyChanged(TextAlignmen

IList<string> IPicker.Items => Items;

bool _itemsLoaded;

int IPicker.SelectedIndex
{
get => SelectedIndex;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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.Issue9150">
<VerticalStackLayout>
<Picker FontSize="15"
AutomationId="picker"
SelectedIndex="1"
x:Name="picker">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>0</x:String>
<x:String>1</x:String>
<x:String>2</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</VerticalStackLayout>
</ContentPage>
15 changes: 15 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue9150.xaml.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.