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);
        }
 
        ///<summary>
        ///To display tapped item content
        ///</summary>
        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");
        }
}

C#

 

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

 

XAML

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

 

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

Screenshot:

Tapped ListViewItem corresponding command in ViewModel.

Click here to download the sample.

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