列表视图页眉和页脚未显示

本文关键字:显示 视图 列表 | 更新日期: 2023-09-27 17:55:59

我在内容页面中有以下列表视图。数据绑定正常,但页眉和页脚永远不会显示。

<?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>

即使我启用分组模板,它也不会显示任何内容。

列表视图页眉和页脚未显示

DataTemplate 包装在 ViewCell 中并为其指定高度:

<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>

我一直遇到同样的问题。 为了解决这个问题,我在视图模型构造函数中实例化了我要绑定到的对象。 我相信这是因为我在使用 <Label Text="{Binding .}"/> 绑定时绑定到一个为空的对象。