【问题标题】:How to update group header from ListView?如何从 ListView 更新组标题?
【发布时间】: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&lt;CustomObject, string&gt; group = p =&gt; p.SomeObject.Identification;

可能的解决方案:

我现在正在做的是通过从原始数据中删除项目并重新设置ItemSource 来完全重新加载ListView。这不是一个真正的高性能解决方案,但它确实有效。 ObservableCollection 的优势已经消失了。

【问题讨论】:

    标签: c# listview xamarin.forms observablecollection


    【解决方案1】:

    假设ObservableGroupCollection 实现INotifyCollectionChanged - 我们可以订阅集合更改事件。要做到这一点,添加可绑定属性会更容易,因为它更容易跟踪属性更改,因此订阅/取消订阅事件。

    public partial class MyGroupingHeaderCell : ViewCell
    {
        public static readonly BindableProperty CollectionProperty =
            BindableProperty.Create(
                "Collection", typeof(INotifyCollectionChanged), typeof(MyGroupingHeaderCell),
                defaultValue: default(INotifyCollectionChanged), propertyChanged: OnCollectionChanged);
    
        public INotifyCollectionChanged Collection
        {
            get { return (INotifyCollectionChanged)GetValue(CollectionProperty); }
            set { SetValue(CollectionProperty, value); }
        }
    
        private static void OnCollectionChanged(BindableObject bindable, object oldValue, object newValue)
        {
            ((MyGroupingHeaderCell)bindable).OnCollectionChangedImpl((INotifyCollectionChanged)oldValue, (INotifyCollectionChanged)newValue);
        }
    
        protected virtual void OnCollectionChangedImpl(INotifyCollectionChanged oldValue, INotifyCollectionChanged newValue)
        {
            if(oldValue != null)
                oldValue.CollectionChanged -= OnCollectionChanged;
            if(newValue != null)
                newValue.CollectionChanged += OnCollectionChanged;
    
            OnCollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    
        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            var item = BindingContext as ObservableGroupCollection<string, CustomObject>;
            if (item != null)
            {
                this.keyName.Text = item.Key + " (" + item.Count + ")";
            }
            else
            {
                this.keyName.Text = string.Empty;
            }
        }
    }
    

    而且,XAML 的用法将更改为:

    <ListView.ItemTemplate>
        <DataTemplate>
            <customViews:MyHeaderCell Collection="{Binding}" />
        </DataTemplate>
    </ListView.ItemTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-19
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      相关资源
      最近更新 更多