ListView Header and Footer not appearing

5.8k Views Asked by At

I have following listview within a content page. Data binds fine, but header and footer never appears.

<?xml version="1.0" encoding="UTF-8" ?>

<ContentPage.Content>
        <SearchBar x:Name="SearchFor"
            Placeholder="search for"
            Text="{Binding Path=SearchText}"
            TextChanged="OnValueChanged" 
            SearchButtonPressed="OnSearch" />

        <ListView x:Name="listView" ItemSelected="OnItemSelected" ItemsSource="{Binding Item}"
Header="{Binding ItemID}"
Footer="{Binding SupplierItemDesc}">
<ListView.HeaderTemplate >
  <DataTemplate>
    <StackLayout Orientation="Horizontal" 
        Padding="10,5,5,10"
        BackgroundColor="Yellow">
      <Label Text="~~"/>
      <Label Text="{Binding .}"/>
      <Label Text="~~"/>
    </StackLayout>
  </DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
    <DataTemplate>
        <TextCell Text="{Binding RetailCurrencyCode}" Detail="{Binding RetailValue}"></TextCell>
    </DataTemplate>
</ListView.ItemTemplate>


</ContentPage.Content>

Even if I enable grouping template, it just won't show anything.

2

There are 2 best solutions below

4
On

Wrap your DataTemplate in a ViewCell and give it a height:

<ListView.HeaderTemplate >
    <DataTemplate>
            <StackLayout Orientation="Horizontal" 
                Padding="10,5,5,10"
                BackgroundColor="Yellow" HeightRequest="40">
                  <Label Text="~~"/>
                  <Label Text="{Binding .}"/>
                  <Label Text="~~"/>
            </StackLayout>
    </DataTemplate>
</ListView.HeaderTemplate>
0
On

I have been having the same issue. What resolved this for me, I instantiated the object I am binding to in the view model constructor. I believe it's because I am binding to an object that is null when binding using <Label Text="{Binding .}"/>.