Articles in this section

How to pass the item data as command parameter in ItemTapped command?

In SfListView, ItemData can only be passed to the view model instead of EvenArgs using EventsToCommand behavior to follow the MVVM pattern.

To achieve this, create a command to the ItemTapped event of SfListView in ViewModel.

C#

 
public class ContactsViewModel
 {
     Command<object> tapCommand;
       
     public Command<object> TapCommand
      {
         get { return tapCommand; }
         protected set { tapCommand = value; }
      }
 
     public ContactsViewModel()
      {
         tapCommand = new Command<object>(OnTapped);
      }
 
     public void OnTapped(object obj)
      {
         var name = (obj as Contacts).ContactName ;
         var alert = Application.Current.MainPage.DisplayAlert("Parameter Passed","Name:" + name,"Cancel");
      }
 
 }
 

 

Associate that command to the appropriate event of SfListView using behaviors.

Xaml

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

 

Here, CustomConverter returns ItemData from ItemTappedEventArgs.

C#

public class CustomConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object eventArgs = null;
            Syncfusion.ListView.XForms.ItemTappedEventArgs eventArg = null;
            if (value is Syncfusion.ListView.XForms.ItemTappedEventArgs)
            {
                eventArg = value as Syncfusion.ListView.XForms.ItemTappedEventArgs;
                eventArgs = eventArg.ItemData;
            }
           
            return eventArgs;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 

When tapping the ListViewItem, ItemData will only be returned from Customconverter to the command in ViewModel and performed the desired operation.

 

Sample Link : TapCommandSample

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied