HeaderTemplate BindingContext change

I have a sflistview and have a set of  ui controls within the headertemplate. These ui controls are bound to a viewmodel properties. When the bindingcontext changes Im not seeing the ui controls properties update. 

Any ideas why?

1 Reply

MK Muthu Kumaran Gnanavinayagam Syncfusion Team January 24, 2018 05:24 PM UTC

Hi Tarek, 
 
We have checked the reported query “Changing the BindingContext of SfListView does not update the elements inside HeaderTemplate” at our end. We would like to let you know that the UI elements present inside the HeaderTemplate will get the BindingContext only during the creation of Header item. So even after changing the BindingContext of SfListView, Header items BindingContext will not be updated. This is because the Header item is simply a template to show the bound values initially. This is the default behavior of SfListView and this behavior is same in Xamarin.Forms ListView. 
 
However you can change the BindingContext of the Header items by extending the ItemGenerator class of SfListView to create a new HeaderItem. Even though the content of HeaderItem which has the UI elements loaded inside HeaderTemplate does not change its BindingContext, the BindingContext of HeaderItem changes. So you can override the OnBindingContextChanged to set the changed BindingContext of HeaderItem to its content as like below code example. 
 
Code Example[C#]: 
    public partial class GroupingPage : ContentPage 
    { 
        public GroupingPage() 
        { 
            InitializeComponent(); 
            this.listView.ItemGenerator = new ItemGeneratorExt(this.listView); 
        } 
    } 
 
    public class ItemGeneratorExt:ItemGenerator 
    { 
         
        public ItemGeneratorExt(SfListView listview) : base(listview) 
        { 
        } 
 
        protected override ListViewItem OnCreateListViewItem(int itemIndex, ItemType type, object data = null) 
        { 
 
            if (type == ItemType.Header) 
                return new HeaderItemExt(); 
            else 
                return base.OnCreateListViewItem(itemIndex, type); 
        } 
    } 
 
    public class HeaderItemExt : HeaderItem 
    { 
        protected override void OnBindingContextChanged() 
        { 
            base.OnBindingContextChanged(); 
 
            if (this.Content != null && this.Content.BindingContext != this.BindingContext) 
                this.Content.BindingContext = this.BindingContext; 
        } 
    } 
 
For your assistance, we have attached a sample and you can download it from the below location. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu kumaran. 


Loader.
Up arrow icon