简体   繁体   English

GroupDisplayBinding 属性导致 ListView.ItemTemplate 消失

[英]GroupDisplayBinding attribute causes the ListView.ItemTemplate to disappear

I'm trying to implement a very simple ListView grouped by an Id value.我正在尝试实现一个非常简单的按 Id 值分组的ListView In my page constructor I've something like this:在我的页面构造函数中,我有这样的东西:

    public CheckApparatusProgressPage(CheckReport checkReport, Apparatus currentApparatus)
    {
        CurrentCheckReport = checkReport;
        CurrentApparatus = currentApparatus;
        InitializeComponent();
        Title = "Check";
        var itemsSource = App.RestClient.GetCheckReportOutcome(CurrentApparatus.Id).Result;
        GroupedView.ItemsSource = itemsSource;
    }

itemsSource is a List<ApparatusCheckStepOutcome> that class looks like this: itemsSource是一个List<ApparatusCheckStepOutcome>类,如下所示:

public class ApparatusCheckStepOutcome
{
    [JsonProperty("apparatusCheckStep")]
    public ApparatusCheckStep ApparatusCheckStep { set; get; }
    [JsonProperty("comment")]
    public string Comment { get; set; }
    [JsonProperty("createdAt")]
    public DateTime CreatedAt { get; set; }
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("passing")]
    public bool Passing { get; set; }
}

And the ApparatusCheckStep class looks like this: ApparatusCheckStep类如下所示:

public class ApparatusCheckStep
{
    [JsonProperty("category")]
    public string Category { get; set; }
    [JsonProperty("createdAt")]
    public DateTime CreatedAt { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("weight")]
    public int Weight { get; set; }
}

Basically I need to show in my group headers the value of {Binding ApparatusCheckStep.Id} and in the children within that group two Labels with {Binding ApparatusCheckStep.Name} and {Binding ApparatusCheckStep.Description} each one.基本上,我需要在我的组标题中显示{Binding ApparatusCheckStep.Id}的值,并在该组内的子组中显示两个带有{Binding ApparatusCheckStep.Name}{Binding ApparatusCheckStep.Description} Labels

This is my XAML view:这是我的 XAML 视图:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="HalliganTL.View.CheckApparatusProgressPage">
    <ContentPage.Content>
      <ListView x:Name="GroupedView" IsGroupingEnabled="True" GroupDisplayBinding="{Binding ApparatusCheckStep.Id}">
      <!-- Single Row Customization -->
      <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
                <Label Text="{Binding ApparatusCheckStep.Name}" FontSize="12" TextColor="White" />
                <Label Text="{Binding ApparatusCheckStep.Description}" FontSize="12" TextColor="White" />
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </ContentPage.Content>
</ContentPage>

I tried everything and I seached lot, if I remove the IsGroupingEnabled and GroupDisplayBinding attributes from the ListView I see all the "Name - Description" rows without any header grouping them.我尝试了所有方法并且搜索了很多,如果我从ListView删除IsGroupingEnabledGroupDisplayBinding属性,我会看到所有“名称 - 描述”行,而没有任何标题对它们进行分组。 If I leave those attributes I only see the Id header rows, and the ListView.ItemTemplate seems to be totally ignored.如果我保留这些属性,我只会看到 Id 标题行,而ListView.ItemTemplate似乎被完全忽略了。

What I'm doing wrong?我做错了什么? I want to keep this as simple as possible, that's why I'm trying to do most of the work with XAML, and I'd like to keep it that way.我想让这尽可能简单,这就是为什么我试图用 XAML 完成大部分工作,我想保持这种方式。

when you use grouping, you need to have your ItemSource in a certain way - you essentially have to have one IEnumerable containing the groups, each of which in turn contains another IEnumerable containing the data for each group当您使用分组时,您需要以某种方式拥有您的 ItemSource - 您基本上必须有一个包含组的 IEnumerable,每个组又包含另一个 IEnumerable,其中包含每个组的数据

Xamarin has a sample app that demonstrates how to do this; Xamarin 有一个示例应用程序来演示如何执行此操作; or there is another excellent example here (dead link, here's an archived version )或者还有另外一个很好的例子在这里(死链接, 这里是一个存档版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListView.ItemTemplate的MultiBinding DataTemplate? - MultiBinding DataTemplate for ListView.ItemTemplate? 按特定值中的值对 ListView 或 ListView.ItemTemplate 进行排序 - Sort a ListView or a ListView.ItemTemplate by a value in a specific 未设置ListView.ItemTemplate并且未触发事件 - ListView.ItemTemplate not being set and events not firing 如何将事件添加到 ListView.ItemTemplate - How to add event to ListView.ItemTemplate ItemTemplateSelector 和 ListView.ItemTemplate 的区别 - Difference between ItemTemplateSelector and ListView.ItemTemplate FindAncestor在ListView.ItemTemplate中不适用于UserControl - FindAncestor does not work for UserControl in ListView.ItemTemplate 将Listview.ItemTemplate的文本传递到另一个框架uwp - Pass text of Listview.ItemTemplate to antoher frame uwp 无法将属性名称动态传递给 ListView.ItemTemplate 以进行绑定 (UWP) - Unable to pass property name dynamically to ListView.ItemTemplate to bind (UWP) Xamarin按钮命令(在ListView.ItemTemplate内部)未触发 - Xamarin Button Command (inside of ListView.ItemTemplate) Not Firing 如何在XAML的ListView.ItemTemplate中引用ListView DataContext? - How can I reference the ListView DataContext from within the ListView.ItemTemplate in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM