Forms:GroupHeaderTemplate{Binding Key}不工作,但ItemTemplate{Bind



我正在用Xamarin表单构建一个移动应用程序。我的问题是无法获取GroupHeader。我检查了所有的平台和网站。我找不到解决问题的办法。我的第一个列表视图运行良好,在第二个列表视图中,我想用groupheaders显示我的值。下面是我的.xaml和.cs代码以及scroonshots。我成功地从web服务中获得了我的所有价值。我的值显示在第二个列表视图中,除了组头。

<ListView x:Name="listview" HasUnevenRows="True" SeparatorVisibility="Default" 
GroupDisplayBinding="{Binding Key}"
IsGroupingEnabled="True" ItemsSource="{Binding GroupedList}"
CachingStrategy="RecycleElement" Margin="0,0,0,15">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell Height="50" >
<Label Text ="{Binding Key}" Margin="0,20,0,0" FontSize="Medium" TextColor="Gray" />
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>

<Grid BackgroundColor="White" Margin="0,0,0,1" >
<Grid.RowDefinitions>
<RowDefinition Height="0.4*"/>
<RowDefinition Height="0.6*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Label  Text="{Binding PayDeskName}"  Grid.Row="0" HorizontalOptions="Start" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Gray"/>
<Label Grid.Column="0" Text="{Binding CollectionType}"  Grid.Row="1" HorizontalOptions="Start" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Blue"/>
<Label Grid.Column="1" Text="{Binding CollectionAmount, StringFormat='{0:N}'}" Grid.Row="1" HorizontalOptions="End" VerticalOptions="Center" FontSize="Large" TextColor="Blue"></Label>

</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

--

using MunIS.Parameters;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;

namespace MunIS.AccrumentCollection
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CollectionPage : ContentPage
{
public CollectionPage()
{
InitializeComponent();
}
public PayDeskBaseCollectionParameters[] CollectionList { get; set; }
public ObservableCollection<Grouping<string, PayDeskBaseCollectionParameters>> GroupedList { get; set; }
public IEnumerable<IGrouping<string, PayDeskBaseCollectionParameters>> CollectionTypeBasedGroupedList { get; set; }
async void GetCollectionList(object sender, EventArgs e)
{
activityStackLayout.IsVisible = true;
activityIndicator.IsRunning = true;
loadingTxt.IsVisible = true;
string queryIntervalStartDate = dtIntervalStart.Date.ToString("MM.dd.yyyy");
string queryIntervalFinishDate = dtIntervalFinish.Date.ToString("MM.dd.yyyy");
CollectionDetailQueryCriteriaParameters criteria = new CollectionDetailQueryCriteriaParameters();
criteria.CollectionDateStart = dtIntervalStart.Date;
criteria.CollectionDateFinish = dtIntervalFinish.Date;
ServiceCaller serviceCaller = new ServiceCaller();
CollectionList = await serviceCaller.ListPayDeskBasedCollections(criteria);

var groupedCollectionList = CollectionList.GroupBy(c => c.CollectionType).Select(
g => new
{
CollectionType = g.Key,
CollectionAmount = g.Sum(s => s.CollectionAmount)
});


GroupedList = new ObservableCollection<Grouping<string, PayDeskBaseCollectionParameters>>(CollectionList.GroupBy(c => c.PayDeskName)
.Select(k => new Grouping<string, PayDeskBaseCollectionParameters>(k.Key + "tt" + CollectionList
.Where(c => c.PayDeskName == k.Key)
.Sum(x => x.CollectionAmount).ToString("N"), k)));

listview1.ItemsSource = groupedCollectionList;
listview.ItemsSource = GroupedList;

activityIndicator.IsRunning = false;
loadingTxt.IsVisible = false;
activityStackLayout.IsVisible = false;


}
}
}

这是我的Grouping.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace MunIS
{
public class Grouping<K, T> : ObservableCollection<T>
{
public K Key { get; set; }
public Grouping(K key, IEnumerable<T> items)
{
Key = key;
foreach (var item in items)
{
this.Items.Add(item);
}
}
}
}

查看我的页面我想获得组标题

这对我有效

从XAML中删除x:DataType属性。出于某种原因,对整个页面这样做会给有自己上下文的模板控件带来问题。

正如Jeroen-Corteville所说,您还需要删除GroupDisplayBinding。两者的作用相同,但在某种程度上不同

我认为您需要从listview元素中删除GroupDisplayBinding。

<ListView x:Name="listview" HasUnevenRows="True" SeparatorVisibility="Default" 
GroupDisplayBinding="{Binding Key}"
IsGroupingEnabled="True" ItemsSource="{Binding GroupedList}"
CachingStrategy="RecycleElement" Margin="0,0,0,15">

此属性与GroupHeaderTemplate属性互斥。设置它会将GroupHeaderTemplate设置为null。

从文档:https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.groupdisplaybinding?view=xamarin-表格#备注

最新更新