We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Sflistview -Set the Bindingcontext for command and command parameter in a listview item

I am using sflistview to display list of items (uses samplelistviewmodel)  and having a datatemplate for my viewcell (uses sampleitemviewmodel)  .
Inside the view cell i have a grid , with a Tap gesture recoognizer .This recognizer would call a command from a samplelistviewmodel and pass the sampleitemviewmodel as a Commandparameter to that command .

                    ItemsSource="{Binding ItemList}"
 SampleViewCell />

My code snippet is as follows,

  <ViewCell.View >  

                            Command="{Binding Source={x:Reference SampleViewCell}, Path=Parent.BindingContext.SampleCommand}"
                            CommandParameter="{Binding Source={x:Reference SampleViewCell}, Path=BindingContext}"
  ViewCell.View >

However the same works , when i try with listview it works , while with sflitview it doesnt.Please help

1 Reply

SP Subburaj Pandian Veluchamy Syncfusion Team May 28, 2019 01:55 PM UTC

Hi Ramya, 
Thank you for contacting Syncfusion support. 
We would like to inform you that in SfListView, BindingContext of the ItemTemplate will be Model class for the Template items. So, when you define the Command for ViewCell, it is necessary to initialize the command in Model as following.  
public class MessageInfo : INotifyPropertyChanged 
       public Command<object> SampleCommand { get; } 
       public MessageInfo() 
            SampleCommand = new Command<object>(OnGridTapped); 
        private void OnGridTapped(object obj) 
            var param = obj as MessageInfo; 
            App.Current.MainPage.DisplayAlert("Message","Tapped Item :"+param.Text,"Ok"); 
<ViewCell > 
    <Grid ColumnSpacing="2" x:Name="Grid"> 
            <TapGestureRecognizer Command="{Binding Source={x:ReferenceGrid}, Path=BindingContext.SampleCommand}" CommandParameter="{Binding Source={x:Reference Grid}, Path=BindingContext}"/> 
</ViewCell > 
We have attached the model sample for your reference, 
Please let us know if you need any further assistance. 
Subburaj Pandian V 

Live Chat Icon For mobile
Up arrow icon