【发布时间】:2017-09-22 11:27:59
【问题描述】:
我正在从我的 ListView 中删除项目。因为我也在组头中使用了一个计数器,所以我必须更新它。但是我该怎么做呢?这是我的代码:
ListView:
<ListView x:Name="MyListList"
HasUnevenRows="True"
CachingStrategy="RecycleElement"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key}"
IsPullToRefreshEnabled="True"
RefreshCommand="{Binding LoadDataCommand}"
IsRefreshing="{Binding IsRefreshing, Mode=OneWay}">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<customViews:MyGroupingHeaderCell/>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<customViews:MyHeaderCell />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
自定义组头:
<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Views.MyGroupingHeaderCell"
xmlns:customViews="clr-namespace:Views;"
xmlns:renderer="clr-namespace:Common.CustomRenderers;">
<renderer:ListViewGroupHeader x:Name="keyName" Style="{StaticResource labelHeader}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ViewCell>
public partial class MyGroupingHeaderCell : ViewCell
{
public MyGroupingHeaderCell()
{
InitializeComponent();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
var item = BindingContext as ObservableGroupCollection<string, CustomObject>;
if (item != null)
{
this.keyName.Text = item.Key + " (" + item.Count + ")";
}
else
{
this.keyName.Text = string.Empty;
}
}
}
我用这个 LINQ 表达式为 item 创建了Key
List<ObservableGroupCollection<string, CustomObject>> resultList = rawList.OrderByWithDirection(order, descending)
.GroupBy(group)
.Select(p => new ObservableGroupCollection<string, CustomObject>(p))
.OrderByWithDirection(i => i.Key, false) // sort the group
.ToList();
group 在哪里
Func<CustomObject, string> group = p => p.SomeObject.Identification;
可能的解决方案:
我现在正在做的是通过从原始数据中删除项目并重新设置ItemSource 来完全重新加载ListView。这不是一个真正的高性能解决方案,但它确实有效。 ObservableCollection 的优势已经消失了。
【问题讨论】:
标签: c# listview xamarin.forms observablecollection