Skip to content

Commit 78cc483

Browse files
committed
feat: Add runtime page size changes
1 parent 1a9eb5d commit 78cc483

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

SimpleDataGrid.Example/MainViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace SimpleDataGrid.Example;
77
public class MainViewModel
88
{
99
public PagedCollection<Person> People { get; }
10+
public List<int> PageSizes { get; } = [10, 25, 50, 100];
1011

1112
public MainViewModel()
1213
{

SimpleDataGrid.Example/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<TextBlock Text="Go to page:" VerticalAlignment="Center" Margin="5,0,0,0"/>
7373
<TextBox x:Name="PageTextBox" Width="30" Margin="5,0,0,0"/>
7474
<Button Content="Go" Click="GoToPageButton_Click" Margin="5,0,0,0"/>
75+
<ComboBox Margin="10,0,0,0" ItemsSource="{Binding PageSizes}" SelectedItem="{Binding People.PageSize}"/>
7576
<TextBlock VerticalAlignment="Center" Margin="10,0,0,0">
7677
<TextBlock.Text>
7778
<MultiBinding Converter="{StaticResource ItemCountConverter}">

SimpleDataGrid/Pagination/PagedCollection.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ namespace SimpleDataGrid.Pagination;
1010
/// <typeparam name="T">The type of items in the collection.</typeparam>
1111
public class PagedCollection<T> : IPagedCollection, INotifyPropertyChanged
1212
{
13-
private readonly int _pageSize;
13+
private int _pageSize;
1414
private int _currentPage;
15+
16+
/// <summary>
17+
/// Occurs when the page size changes.
18+
/// </summary>
19+
public event EventHandler? PageSizeChanged;
1520
/// <summary>
16-
/// Gets the number of items to display per page.
21+
/// Gets or sets the number of items to display per page.
1722
/// </summary>
18-
public int PageSize => _pageSize;
23+
public int PageSize
24+
{
25+
get => _pageSize;
26+
set => SetPageSize(value);
27+
}
1928
private IReadOnlyList<T> _source = [];
2029
private IReadOnlyList<T> _filtered = [];
2130

@@ -295,6 +304,22 @@ public void GoToLastPage()
295304
GoToPage(TotalPages);
296305
}
297306

307+
/// <summary>
308+
/// Sets the number of items to display per page.
309+
/// </summary>
310+
/// <param name="newSize">The new page size.</param>
311+
public void SetPageSize(int newSize)
312+
{
313+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newSize);
314+
315+
var oldFirstItemIndex = _currentPage * _pageSize;
316+
_pageSize = newSize;
317+
_currentPage = oldFirstItemIndex / _pageSize;
318+
319+
RaiseAllChanged();
320+
PageSizeChanged?.Invoke(this, EventArgs.Empty);
321+
}
322+
298323
private void RaiseAllChanged()
299324
{
300325
OnPropertyChanged(nameof(CurrentPageItems));

0 commit comments

Comments
 (0)