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.
Unfortunately, activation email could not send to your email. Please try again.

Accessing items in headertemplate from code behind

Thread ID:

Created:

Updated:

Platform:

Replies:

131680 Jul 23,2017 08:44 AM Jul 24,2017 06:51 AM Xamarin.Forms 1
loading
Tags: SfListView
Emil
Asked On July 23, 2017 08:44 AM

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?

Dinesh Babu Yadav [Syncfusion]
Replied On July 24, 2017 06:51 AM

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 
 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;