У меня есть представление с 2-мя различными ListViews, оба фильтруются следующим образом:
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">
The 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);
}
}
Thisработает отлично, но теперь я хочу добавить нумерацию страниц.
Я нашел этот класс в Интернете, он работает для нумерации страниц, я получаю всю коллекцию, и он показывает только количество, указанное в качестве параметра.Проблема в том, что фильтр работает только для коллекции, которая видна в виде списка, например: 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;
}
}