You can handle item selection by subscribing to the ListView’s ItemSelected event. When a user selects an item, the event is triggered, and the OnItemSelected event handler is called, which retrieves the selected item.
XAML
<ListView ItemsSource="{Binding Books}"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Title}" Detail="{Binding Description}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#
private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
// Handle the selected item (e.SelectedItem) here
var selectedBook = e.SelectedItem as Book;
// Do something with the selected book, e.g., display details
DisplayAlert("Selected Book", $"Title:{selectedBook.Title}\nDescription: {selectedBook.Description}", "OK");
// Clear the selection
((ListView)sender).SelectedItem = null;
}
Share with