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?


<sfListView:SfListView.GroupHeaderTemplate>
                    <DataTemplate>
<Label x:Name="lblName" />
     </DataTemplate>
</sfListView:SfListView.GroupHeaderTemplate>

For example Xamarin Forms ListView doesnt require

 <ListView>
                <ListView.Header>
                    
                </ListView.Header>
            </ListView>

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}"> 
  <listView:SfListView.HeaderTemplate> 
    <DataTemplate> 
      <Grid BackgroundColor="Teal"> 
        <Label Text="{Binding HeaderText,Mode=TwoWay}" TextColor="White" 
               FontSize="18" LineBreakMode="NoWrap"/> 
      </Grid> 
    </DataTemplate> 
  </listView:SfListView.HeaderTemplate> 
</listView:SfListView> 
 
 
Code Example[C#]: 
public string headertext; 
   
public string HeaderText 
{ 
  get { return headertext; } 
  set 
  { 
    headertext = value; 
    RaisedOnPropertyChanged("HeaderText"); 
  } 
} 
 
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. 
 
Regards, 
Dinesh Babu Yadav 
 


Loader.
Up arrow icon