Colleagues, welcome! Please help me figure out when I click on the ListView, how to find out the selected value (I want to save it to a separate list), I try to implement the click processing logic of the ViewModel type, but I don’t understand how to use the SelectedItem = "{Binding SelectItem}"

Thank you in advance for your response

XAML pages

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App5.Views.KatalogLVPage"> <ListView x:Name="MyListView" ItemTapped="Handle_ItemTapped" SelectedItem="{Binding SelectItem}" CachingStrategy="RecycleElement" HasUnevenRows="True" ItemsSource="{Binding ItemsList}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Horizontal"> <Image Source="{Binding ImageName}" WidthRequest="45" HeightRequest="60" /> <StackLayout> <Label Text="{Binding Name}" FontSize="18" /> <Label Text="{Binding CountOneitem}" /> <Label Text="{Binding Rub}" /> </StackLayout> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> 

    1 answer 1

    In the ViewModel, you need to create a getter / setter method:

      private Item _selectedItem; public Item SelectItem { get { return _selectedItem; } set { _selectedItem = value; if (_selectedItem == null) return; YourMethod(item); SelectedItem = null; } } 

    The setter will be called when the selected item changes .

    value - the item that was selected

    And do not forget to specify the binding path in KatalogLVPage.cs :

     public KatalogLVPage() { InitializeComponent(); BindingContext = (ваша экземпляр ViewModel); } 

    You can find out more about the MVVM pattern and binding here: https://docs.microsoft.com/ru-ru/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

    • Thank you so much, like the answer I met on the Internet, but he was wise, everything is simple, thank you for the answer! =) - Denis