I make a demo and it works well.
First, you should create a model
class. Here is the code in Photo.cs.
public class Photo
{
public string Name { get; set; }
}
Second, you can create a ViewModel
to handle the data. Here is the code in MyViewModel.cs
public class MyViewModel
{
public ObservableCollection<Photo> Photos { get; set; }
public MyViewModel()
{
Photos = new ObservableCollection<Photo>();
Photos.Add(new Photo() { Name = "Name1", });
Photos.Add(new Photo() { Name = "Name2", });
Photos.Add(new Photo() { Name = "Name3", });
}
}
Last, you should bind the ViewModel to the mainpage. Here is the code in MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
BindingContext = new MyViewModel();
InitializeComponent();
}
void OnPickerSelectedIndexChanged(object sender, EventArgs e)
{
var picker = (Picker)sender;
int selectedIndex = picker.SelectedIndex;
if (selectedIndex != -1)
{
labelname.Text = picker.Items[selectedIndex];
}
}
}
and here is the code in MainPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp7"
x:Class="MauiApp7.MainPage">
<StackLayout>
<Picker Title="Select a photo"
ItemsSource="{Binding Photos}"
ItemDisplayBinding="{Binding Name}"
SelectedIndexChanged="OnPickerSelectedIndexChanged"/>
<Label x:Name="labelname" />
</StackLayout>
</ContentPage>
You can refer to this link Picker for more information.