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
close icon

I need to get the currentItem from the controls inside the itemTemplate

Good morning, I have an SfListView with controls defined for each record within ItemTemplate and I would like to be able to obtain the currentItem when each control is accessed within the template, for possible validations. This is possible?

5 Replies

GP Gnana Priya Namasivayam Syncfusion Team December 5, 2019 12:27 PM UTC

Hi Victor, 
 
Thank you for using Syncfusion product. 
 
We would like to inform that you can CurrentItem of SfListview through the commands/events like ItemTapped, ItemDoubleTapped,SelectionChanged, SelectionChanging etc., like below. We have attached the code snippet and documentation for your reference, please find it from below. 
 
 
 
 
 
 
public class Behavior : Behavior<SfListView> 
{ 
    SfListView Listview; 
    protected override void OnAttachedTo(SfListView bindable) 
    { 
       Listview = bindable; 
       Listview.ItemTapped += Listview_ItemTapped; 
 
       base.OnAttachedTo(bindable); 
    } 
         
    private void Listview_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e) 
    { 
       var listview = sender as SfListView; 
       var currentItem = listview.CurrentItem as BookInfo; 
       if (currentItem == null) 
          return; 
       App.Current.MainPage.DisplayAlert("Message", "CurrentItem of listview: " + currentItem.BookName, "Ok"); 
    } 
} 
 
 
Please let us know if you need any further assistance. 
 
Regards, 
Gnana Priya N 



VR Victor Rafael Bocanegra Arias December 5, 2019 07:05 PM UTC

Thank you for the prompt response, it does work, but it has a problem. Within the ItemTemplate of the SfListView I have three controls: CheckBox, Entry and SfNumericTextBox, when I tap on one of them, the event does not trigger me. This is normal? Any chance so that when you tap the control it extends to its predecessor?


GP Gnana Priya Namasivayam Syncfusion Team December 6, 2019 11:38 AM UTC

Hi Victor, 
 
We would like to inform that when you tap interactable control loaded inside the ItemTemplate, touch will pass only to the respective child control. Here, touch will pass to parent only when tapped on any layouts other than controls inside the ItemTemplate. This is the actual behavior.  
 
Meanwhile, we suggest you to hook to the respected control’s tap/any interaction event in order to get the BindingContext (Model class) of that control through which you can get the binded model data. 
 
Regards, 
Gnana Priya N 



VR Victor Rafael Bocanegra Arias December 6, 2019 12:17 PM UTC

Thank you for the excellent response, I don't know if you could help me with an example please! XD


GP Gnana Priya Namasivayam Syncfusion Team December 9, 2019 04:54 PM UTC

Hi Victor,  
 
We have prepared the sample in which we have displayed a previous item’s binding context value for Entry and Button like below. We have attached the tested sample for your reference, please find the sample from below. 
 
 
 
public partial class MainPage : ContentPage 
{ 
        BookInfo previousBtnItem=null, previousEntryItem=null,currentEntryItem,currentButtonItem; 
        public MainPage() 
        { 
            InitializeComponent(); 
        } 
 
        private void Entry_Focused(object sender, FocusEventArgs e) 
        { 
            var bcEntry = (sender as Entry).BindingContext as BookInfo; 
 
            currentEntryItem = bcEntry; 
 
            if (previousEntryItem!=null) 
            { 
                DisplayAlert("Message", "Previous entry item " + previousEntryItem.BookName, "Ok"); 
 
            } 
             previousEntryItem= currentEntryItem; 
 
        } 
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
            var bcButton = (sender as Button).BindingContext as BookInfo; 
            currentButtonItem = bcButton; 
 
            if (previousBtnItem != null) 
            { 
                DisplayAlert("Message", "Previous entry item " + previousBtnItem.BookName, "Ok"); 
 
            } 
            previousBtnItem= currentButtonItem; 
 
        } 
} 
 
 
Please let us know if you need any further assistance. 
 
Regards, 
Gnana Priya N 


Loader.
Live Chat Icon For mobile
Up arrow icon