Articles in this section
Category / Section

How to turn Events into Commands with Behaviors in SfListView?

1 min read

In SfListView, Event can be turned into the Command using Behaviors to follow the MVVM pattern.

To achieve this, need to create a Command for the ItemTapped event of SfListView in ViewModel.

public class ContactsViewModel
        Command<ItemTappedEventArgs> tapCommand;
        public Command<ItemTappedEventArgs> TapCommand
            get { return tapCommand; }
            protected set { tapCommand = value; }
        public ContactsViewModel()
            tapCommand = new Command<Syncfusion.ListView.XForms.ItemTappedEventArgs>(OnTapped);
        ///To display tapped item content
        public void OnTapped(ItemTappedEventArgs eventArgs)
            var name =(eventArgs.ItemData as Contacts).ContactName;
            var number = (eventArgs.ItemData as Contacts).ContactNumber;
            var display = Application.Current.MainPage.DisplayAlert(name, "Number:"+number, "Ok");



And associate that Command to the appropriate Event of SfListView using Behaviors like below.



<syncfusion:SfListView x:Name="listView" >
    <local:EventToCommandBehavior EventName="ItemTapped"
                                  Command="{Binding TapCommand}"
                                  Converter="{StaticResource EventArgs}" >


Now, when tapped on the ListViewItem, corresponding Command in ViewModel will be invoked automatically and performed the desired operation as shown in below screenshot.


Tapped ListViewItem corresponding command in ViewModel.

Click here to download the sample.

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Please  to leave a comment
Access denied
Access denied