Skip to content

SyncfusionExamples/xamarin-forms-listview-itemtapped-mvvm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Handling ItemTapped event in ListView

ListView supports binding the TapCommand property with the item taped action from view model, where you can write navigation or any other action code in the execution. When defining the command, ItemTappedEventArgs will be passed as command parameter which has item information in execution.

You can define the command parameter for TapCommand using TappedCommandParameter, where you can get the element reference passed in view model.

<syncfusion:SfListView x:Name="listView" 
                    TapCommand="{Binding TapCommand}"
                    ItemsSource="{Binding BookInfoCollection}"/>
//ViewModel.cs

public class BookInfoRepository : INotifyPropertyChanged
{
    private Command tapCommand;

    public Command TapCommand
    {
        get { return tapCommand; }
        protected set { tapCommand = value; }
    }

    public BookInfoRepository()
    {
        tapCommand = new Command(OnItemTapped);
    }

    ///<summary>
    ///To display tapped item content
    ///</summary>
    public void OnItemTapped(object obj)
    {
        var eventArgs = obj as Syncfusion.ListView.XForms.ItemTappedEventArgs;
        var bookName = (eventArgs.ItemData as BookInfo).BookName;
        var bookDescription = (eventArgs.ItemData as BookInfo).BookDescription;
        var display = Application.Current.MainPage.DisplayAlert(bookName, "Description:" + bookDescription, "Ok");
    }
}

To know more about MVVM in ListView, please refer our documentation here

About

This repository contains the sample which shows binding of ItemTapped event to ViewModel in Xamarin.Forms ListView?

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages