[英]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
删除IsGroupingEnabled
和GroupDisplayBinding
属性,我会看到所有“名称 - 描述”行,而没有任何标题对它们进行分组。 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.