2017-09-22 11 views
0

私はListViewからアイテムを削除しています。私もグループヘッダーのカウンターを使用するので、私はそれを更新する必要があります。しかし、私はこれをどのようにすることができますか?これは私のコードです: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式

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(); 
項目ため Keyを作成します私が今やっていることは、私は完全に元のデータから項目を削除し、再設定することで、 ListViewをリロードということです

group

Func<CustomObject, string> group = p => p.SomeObject.Identification;

考えられるソリューションです

ItemSource。これは本当に完璧な解決策ではありませんが、機能します。しかし、ObservableCollectionの利点はなくなりました。

答えて

1

ObservableGroupCollectionINotifyCollectionChangedを実装すると仮定すると、コレクションの変更イベントを購読することができます。これを行うには、プロパティの変更を追跡し、イベントを購読/購読解除する方が簡単なので、バインド可能なプロパティを追加する方が簡単です。

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> 
関連する問題