fix: make sure page count is properly calculated even if template is not loaded.

This commit is contained in:
rabbitism
2024-08-10 13:37:35 +08:00
parent 642ad4964e
commit 378c5b0200
2 changed files with 12 additions and 7 deletions

View File

@@ -30,7 +30,8 @@
TotalCount="600" />
<TextBlock Text="Tiny Pagination"/>
<u:Pagination Theme="{DynamicResource TinyPagination}" TotalCount="100" PageSize="1"/>
<u:Pagination Theme="{DynamicResource TinyPagination}" Command="{Binding LoadPageCommand}"
CommandParameter="{Binding $self.CurrentPage}" PageSize="1" TotalCount="100" />
<u:Pagination Classes="ReadOnly" Theme="{DynamicResource TinyPagination}" TotalCount="100" PageSize="1"/>
</StackPanel>
</UserControl>

View File

@@ -114,8 +114,9 @@ public class Pagination: TemplatedControl
private int _pageCount;
public static readonly DirectProperty<Pagination, int> PageCountProperty = AvaloniaProperty.RegisterDirect<Pagination, int>(
nameof(PageCount), o => o.PageCount);
public static readonly DirectProperty<Pagination, int> PageCountProperty =
AvaloniaProperty.RegisterDirect<Pagination, int>(
nameof(PageCount), o => o.PageCount, (o, e) => o.PageCount = e);
/// <summary>
/// Page count.
@@ -291,11 +292,14 @@ public class Pagination: TemplatedControl
/// <param name="page"></param>
private void UpdateButtonsByCurrentPage(int? page)
{
if (_buttonPanel is null) return;
if (PageSize == 0) return;
int? currentPage = CurrentPage;
int pageCount = TotalCount / PageSize;
if (_buttonPanel is null)
{
SetCurrentValue(PageCountProperty, pageCount);
return;
}
int? currentPage = CurrentPage;
int residue = TotalCount % PageSize;
if (residue > 0)
{
@@ -360,7 +364,7 @@ public class Pagination: TemplatedControl
}
}
PageCount = pageCount;
SetCurrentValue(PageCountProperty, pageCount);
SetCurrentValue(CurrentPageProperty, currentPage);
if (_previousButton != null) _previousButton.IsEnabled = (CurrentPage ?? int.MaxValue) > 1;
if (_nextButton != null) _nextButton.IsEnabled = (CurrentPage ?? 0) < PageCount;