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

OnBindingContextChanged method not called in ViewCell

Hi,
I'm having a situation where the onBindingContextChanged method is not called in a ViewCell used in the Syncfusion sfListView through a DataTemplateSelector.
When I use the regular Xamarin.Forms ListView (commented in code below) the OnBindingContextChanged method is called and everything works fine.

Page with sfListView (binding is done by means of a ViewModel):

<syncfusion:SfListView     
    
ItemsSource="{Binding DataList}"
     ItemTemplate="{StaticResource TotalDataTemplateSelector}"
    
AutoFitMode="Height"
    
SelectionMode="None"
    
ItemSize="300">
</syncfusion:SfListView>


The used DataTemplateSelector:

public class TotalDataTemplateSelector : DataTemplateSelector

{

     private readonly DataTemplate _detailTotalDataTemplate;
     private readonly DataTemplate _generalTotalDataTemplate;

     public TotalDataTemplateSelector(bool expand)

       {

           _generalTotalDataTemplate = new DataTemplate(typeof(TotalTileViewCell));
          
_detailTotalDataTemplate = new DataTemplate(typeof(AccordionDetailTotalViewCell));

           _detailTotalDataTemplate.SetValue(AccordionDetailTotalViewCell.ExpandedProperty, expand);

       }

     protected override DataTemplate OnSelectTemplate(object item, BindableObject container)

       {

           if (item is GeneralTotal)
               retu
rn _generalTotalDataTemplate;

            

           if (item is DetailTotal)
               return _detailTotalDataTemplate;

       }

}


The ViewCell itself is just a regular Xamarin.Forms ViewCell with some stacklayout and labels in it. I need the OnBindingContextChanged method because these viewcell are dynamic and I have to take actions depending on the values of the binded object.


Any ideas why the OnBindingContextChanged method in the code behind of the ViewCell isn't called?
Thx



1 Reply

DB Dinesh Babu Yadav Syncfusion Team October 2, 2017 05:43 AM UTC

Hi Peter, 
 
Thank you for using Syncfusion Products. 
 
In Xamarin Forms ListView, ViewCell has been internally handled to raise the OnBindingContextChanged event and override method whenever the binding context of item is changed. But in SfListView, we have updated the binding context of the ListViewItem only and it raises the View’s bindingcontext event if loaded in ViewCell  as like below code example in which you can dynamically modify the data for the item. 
 
Code Example[C#]: 
public IncomingViewCell() 
{ 
   InitializeComponent(); 
   this.View.BindingContextChanged += View_BindingContextChanged; 
} 
 
private void View_BindingContextChanged(object sender, EventArgs e) 
{ 
   //Modify the data here 
   var grid = sender as Grid; 
} 
 
For your reference, we have attached the sample and you can download it from the below link. 
 
 
Please let us know if you require further assistance. 
 
Dinesh Babu Yadav 
 


Loader.
Up arrow icon