具有过滤功能的WPF MVVM Listview分页

时间:2018-12-10 12:38:17

标签: wpf mvvm

我有一个具有2个不同Listview的视图,两个视图均通过以下方式过滤:

XAML:

<ListView x:Name="lstAlleItems" ItemsSource="{Binding SourceCollection}" ItemContainerStyle="{StaticResource TriggerItem}" SelectedItem="{Binding SelectedItem}" SelectionMode="Single" ScrollViewer.CanContentScroll="False" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Stretch" Margin="0,0,100,10" Height="390" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn>
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox BorderBrush="#00567F" IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Titel" DisplayMemberBinding="{Binding Titel}" Width="300"/>
                            <GridViewColumn Header="Datum" DisplayMemberBinding="{Binding Datum}" Width="200"/>
                            <GridViewColumn DisplayMemberBinding="{Binding Thema}" Width="150">

ViewModel:

        public ObservableCollection<Items> items { get; set; }

        FilterCollection = new CollectionViewSource();
        FilterCollection.Source = items;
        FilterCollection.Filter += FilterCollection_Filter_Verify;
        FilterCollection.Filter += FilterCollection_Filter_Datum;
        FilterCollection.Filter += FilterCollection_Filter_Titel; 


    void FilterCollection_Filter_Titel(object sender, FilterEventArgs e)
    {
        Items item = e.Item as Items;
        if (!string.IsNullOrEmpty(FilterText))
        {
            e.Accepted &= item.Titel.Contains(FilterText); 
        }
    }

这工作正常,但现在我想添加分页。

我在互联网上找到了这个课程,它可以用于分页工作,我可以获取整个收藏集,并且只显示金额 多数民众赞成作为参数。问题在于过滤器仅适用于列表视图中可见的集合,例如: PaginatedObservableCollection(20)

(每页显示20个项目) 该过滤器将仅过滤20个项目,而不过滤整个集合,然后显示前20个结果。 如何扩展此类以过滤整个集合,然后添加分页? 还是他们这样做的更好方法?欢迎所有建议。

PaginatedObservableCollection:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace Kennisbank_F2
{
public class PaginatedObservableCollection<T> : ObservableCollection<T>
{
    #region Properties
    public int PageSize
    {
        get { return _itemCountPerPage; }
        set
        {
            if (value >= 0)
            {
                _itemCountPerPage = value;
                RecalculateThePageItems();
                OnPropertyChanged(new PropertyChangedEventArgs("PageSize"));
            }
        }
    }

    public int CurrentPage
    {
        get { return _currentPageIndex; }
        set
        {
            if (value >= 0)
            {
                _currentPageIndex = value;
                RecalculateThePageItems();
                OnPropertyChanged(new 
                PropertyChangedEventArgs("CurrentPage"));
            }
        }
    }

    #endregion

    #region Constructor
    public PaginatedObservableCollection(IEnumerable<T> collecton)
    {
        _currentPageIndex = 0;
        _itemCountPerPage = 1;
        originalCollection = new List<T>(collecton);
        RecalculateThePageItems();
    }

    public PaginatedObservableCollection(int itemsPerPage)
    {
        _currentPageIndex = 0;
        _itemCountPerPage = itemsPerPage;
        originalCollection = new List<T>();
    }

    public PaginatedObservableCollection()
    {
        _currentPageIndex = 0;
        _itemCountPerPage = 1;
        originalCollection = new List<T>();
    }
    #endregion

    #region private 
    private void RecalculateThePageItems()
    {
        Clear();

        int startIndex = _currentPageIndex * _itemCountPerPage;

        for (int i = startIndex; i < startIndex + _itemCountPerPage; i++)
        {
            if (originalCollection.Count > i)
                base.InsertItem(i - startIndex, originalCollection[i]);
        }
    }
    #endregion

    #region Overrides

    protected override void InsertItem(int index, T item)
    {
        int startIndex = _currentPageIndex * _itemCountPerPage;
        int endIndex = startIndex + _itemCountPerPage;

        //Check if the Index is with in the current Page then add to the 
          collection as bellow. And add to the originalCollection also
        if ((index >= startIndex) && (index < endIndex))
        {
            base.InsertItem(index - startIndex, item);

            if (Count > _itemCountPerPage)
                base.RemoveItem(endIndex);
        }

        if (index >= Count)
            originalCollection.Add(item);
        else
            originalCollection.Insert(index, item);
    }

    protected override void RemoveItem(int index)
    {
        int startIndex = _currentPageIndex * _itemCountPerPage;
        int endIndex = startIndex + _itemCountPerPage;
        //Check if the Index is with in the current Page range then remove 
         from the collection as bellow. And remove from the 
         originalCollection also
        if ((index >= startIndex) && (index < endIndex))
        {
            base.RemoveAt(index - startIndex);

            if (Count <= _itemCountPerPage)
                base.InsertItem(endIndex - 1, originalCollection[index + 
                1]);
        }

        originalCollection.RemoveAt(index);
    }

    #endregion

    private List<T> originalCollection;
    private int _currentPageIndex;
    private int _itemCountPerPage;

    }
 }

0 个答案:

没有答案