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

Accessing items in headertemplate from code behind

Why SfListView requires a datatemplate for header?

<Label x:Name="lblName" />

For example Xamarin Forms ListView doesnt require


Problem here is if I have label like above, I cannot access it from code behind directly like lblName.Text="Test";, however in XF listView it is possible to do this. Can you please let me know what is the difference and how can I achieve this?

1 Reply

DB Dinesh Babu Yadav Syncfusion Team July 24, 2017 10:51 AM UTC

Hi Emil, 
Thank you for contacting Syncfusion support. 
We would like to let you know that you need to define the custom view in the DataTemplate only for HeaderItem and this is the actual behavior in SfListView. Also, the reported requirement “Accessing Header item in code behind in SfListView” can be achieved by using binding concept. In the sample, you need to have a property in the ViewModel class and bind it to the custom view in the HeaderTempate as like below code snippet with Mode as “TwoWay”. So, whenever you change the property at runtime, the desired data gets updated in header item as expected. 
Code Example[XAML]: 
<listView:SfListView x:Name="listView" ItemSize="70" HeaderSize="50" 
                     ItemsSource="{Binding Contactsinfo}"> 
      <Grid BackgroundColor="Teal"> 
        <Label Text="{Binding HeaderText,Mode=TwoWay}" TextColor="White" 
               FontSize="18" LineBreakMode="NoWrap"/> 
Code Example[C#]: 
public string headertext; 
public string HeaderText 
  get { return headertext; } 
    headertext = value; 
public ContactsViewModel() 
   headertext = "SfListView Header"; 
   contactsinfo = new ObservableCollection<Contacts>(); 
Code Example[C#]: 
private void headerchanged_Clicked(object sender, EventArgs e) 
  ViewModel.HeaderText = "Contacts Header"; 
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 

Live Chat Icon For mobile
Up arrow icon