Skip to content

Commit c927f83

Browse files
committed
A bit more stuff
1 parent 0d1faed commit c927f83

File tree

5 files changed

+93
-30
lines changed

5 files changed

+93
-30
lines changed

SimpleDataGrid/Class1.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using SimpleDataGrid.Pagination;
2+
using System.Windows.Controls;
63

7-
namespace SimpleDataGrid.Pagination;
8-
internal class DataGridExtensions
4+
namespace SimpleDataGrid.Extensions;
5+
6+
public static class DataGridExtensions
97
{
8+
public static void BindPagedSource<T>( this DataGrid grid, PagedCollectionView<T> pagedCollection)
9+
=> grid.ItemsSource = pagedCollection.GetCurrentPage();
10+
11+
public static void RefreshPagedSource<T>(
12+
this DataGrid grid,
13+
PagedCollectionView<T> pagedCollection)
14+
{
15+
grid.ItemsSource = null;
16+
grid.ItemsSource = pagedCollection.GetCurrentPage();
17+
}
1018
}
Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace SimpleDataGrid.Pagination;
8-
internal class FilterCollection
1+
namespace SimpleDataGrid.Filtering;
2+
3+
public class FilterCollection<T>(IEnumerable<T> items)
94
{
5+
private readonly ObservableCollection<T> _source = new(items);
6+
private readonly List<Func<T, bool>> _filters = [];
7+
8+
public void AddFilter(Func<T, bool> filter) => _filters.Add(filter);
9+
10+
public void ClearFilters() => _filters.Clear();
11+
12+
public IEnumerable<T> ApplyFilters()
13+
{
14+
IEnumerable<T> query = _source;
15+
16+
foreach (var filter in _filters)
17+
{
18+
query = query.Where(filter);
19+
}
20+
21+
return query;
22+
}
23+
24+
public void Add(T item) => _source.Add(item);
25+
26+
public void Remove(T item) => _source.Remove(item);
27+
28+
public void Clear() => _source.Clear();
1029
}

SimpleDataGrid/GlobalUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using System.Collections.ObjectModel;
Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace SimpleDataGrid.Pagination;
8-
internal class PagedCollectionView
1+
namespace SimpleDataGrid.Pagination;
2+
3+
public class PagedCollectionView<T>
94
{
5+
private readonly IEnumerable<T> _items;
6+
7+
public int PageSize { get; }
8+
public int CurrentPage { get; private set; }
9+
public int TotalItems => _items.Count();
10+
public int TotalPages => (int)Math.Ceiling((double)TotalItems / PageSize);
11+
12+
public PagedCollectionView(IEnumerable<T> items, int pageSize = 10)
13+
{
14+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pageSize);
15+
16+
_items = items ?? throw new ArgumentNullException(nameof(items));
17+
PageSize = pageSize;
18+
CurrentPage = 1;
19+
}
20+
21+
public IEnumerable<T> GetCurrentPage() => _items
22+
.Skip((CurrentPage - 1) * PageSize)
23+
.Take(PageSize);
24+
25+
public bool MoveNextPage()
26+
{
27+
if (CurrentPage < TotalPages)
28+
{
29+
CurrentPage++;
30+
return true;
31+
}
32+
return false;
33+
}
34+
35+
public bool MovePreviousPage()
36+
{
37+
if (CurrentPage > 1)
38+
{
39+
CurrentPage--;
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
public void MoveToPage(int pageNumber)
46+
{
47+
if (pageNumber < 1 || pageNumber > TotalPages)
48+
throw new ArgumentOutOfRangeException(nameof(pageNumber));
49+
50+
CurrentPage = pageNumber;
51+
}
1052
}

0 commit comments

Comments
 (0)