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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

144873 May 27,2019 10:25 AM UTC May 28,2019 01:55 PM UTC Xamarin.Forms 1
loading
Tags: SfListView
Ramya
Asked On May 27, 2019 10:28 AM UTC

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 .

  <xForms:SfListView
                    Grid.Row="2"
                    SelectionBackgroundColor="Transparent"
                    BackgroundColor="#F0F4F7"
                    ItemSize="270"
                    ItemsSource="{Binding ItemList}"
                   
                   >
                    <xForms:SfListView.ItemTemplate>
                        <DataTemplate>
                            <views:
 SampleViewCell />
                        DataTemplate>
                    xForms:SfListView.ItemTemplate>
                xForms:SfListView>


My code snippet is as follows,

<ViewCell
    x:Class=""
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls=""
    xmlns:xamlMarkupExtensions=""
    x:Name="
SampleViewCell"
   >
  
  <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

Subburaj Pandian Veluchamy [Syncfusion]
Replied On 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.  
  
  
MessageInfoModel.cs 
 
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"); 
        } 
} 
  
IncomingTextTemplate.xaml 
  
<ViewCell > 
    <Grid ColumnSpacing="2" x:Name="Grid"> 
        <Grid.GestureRecognizers> 
            <TapGestureRecognizer Command="{Binding Source={x:ReferenceGrid}, Path=BindingContext.SampleCommand}" CommandParameter="{Binding Source={x:Reference Grid}, Path=BindingContext}"/> 
        </Grid.GestureRecognizers> 
</ViewCell > 
  
  
We have attached the model sample for your reference, 
  
 
Please let us know if you need any further assistance. 
 
Regards,
Subburaj Pandian V 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon