Merge pull request #339 from irihitech/pagination
Pagination new Theme: TinyPagination
This commit is contained in:
@@ -28,5 +28,10 @@
|
|||||||
Command="{Binding LoadPageCommand}"
|
Command="{Binding LoadPageCommand}"
|
||||||
CommandParameter="{Binding $self.CurrentPage}"
|
CommandParameter="{Binding $self.CurrentPage}"
|
||||||
TotalCount="600" />
|
TotalCount="600" />
|
||||||
|
|
||||||
|
<TextBlock Text="Tiny Pagination"/>
|
||||||
|
<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>
|
</StackPanel>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="u:Pagination">
|
<ControlTemplate TargetType="u:Pagination">
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal">
|
||||||
<u:PaginationButton u:DisabledAdorner.IsEnabled="True" Name="{x:Static u:Pagination.PART_PreviousButton}">
|
<u:PaginationButton Name="{x:Static u:Pagination.PART_PreviousButton}" u:DisabledAdorner.IsEnabled="True">
|
||||||
<PathIcon
|
<PathIcon
|
||||||
Width="12"
|
Width="12"
|
||||||
Height="12"
|
Height="12"
|
||||||
@@ -20,21 +20,24 @@
|
|||||||
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
||||||
</u:PaginationButton>
|
</u:PaginationButton>
|
||||||
<StackPanel Name="{x:Static u:Pagination.PART_ButtonPanel}" Orientation="Horizontal" />
|
<StackPanel Name="{x:Static u:Pagination.PART_ButtonPanel}" Orientation="Horizontal" />
|
||||||
<u:PaginationButton u:DisabledAdorner.IsEnabled="True" Name="{x:Static u:Pagination.PART_NextButton}">
|
<u:PaginationButton Name="{x:Static u:Pagination.PART_NextButton}" u:DisabledAdorner.IsEnabled="True">
|
||||||
<PathIcon
|
<PathIcon
|
||||||
Width="12"
|
Width="12"
|
||||||
Height="12"
|
Height="12"
|
||||||
Data="{DynamicResource PaginationForwardGlyph}"
|
Data="{DynamicResource PaginationForwardGlyph}"
|
||||||
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
||||||
</u:PaginationButton>
|
</u:PaginationButton>
|
||||||
<StackPanel Orientation="Horizontal" IsVisible="{TemplateBinding ShowQuickJump}">
|
<StackPanel IsVisible="{TemplateBinding ShowQuickJump}" Orientation="Horizontal">
|
||||||
<TextBlock
|
<TextBlock
|
||||||
Margin="4 0"
|
Margin="4,0"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
Text="{DynamicResource STRING_PAGINATION_JUMP_TO}" />
|
Text="{DynamicResource STRING_PAGINATION_JUMP_TO}" />
|
||||||
<u:NumericIntUpDown x:Name="{x:Static u:Pagination.PART_QuickJumpInput}" ShowButtonSpinner="False" Width="50"></u:NumericIntUpDown>
|
<u:NumericIntUpDown
|
||||||
|
x:Name="{x:Static u:Pagination.PART_QuickJumpInput}"
|
||||||
|
Width="50"
|
||||||
|
ShowButtonSpinner="False" />
|
||||||
<TextBlock
|
<TextBlock
|
||||||
Margin="4 0"
|
Margin="4,0"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
Text="{DynamicResource STRING_PAGINATION_PAGE}" />
|
Text="{DynamicResource STRING_PAGINATION_PAGE}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
@@ -48,6 +51,63 @@
|
|||||||
</Setter>
|
</Setter>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
|
<ControlTheme x:Key="TinyPagination" TargetType="{x:Type u:Pagination}">
|
||||||
|
<Setter Property="DisplayCurrentPageInQuickJumper" Value="True"></Setter>
|
||||||
|
<Setter Property="Template">
|
||||||
|
<ControlTemplate TargetType="u:Pagination">
|
||||||
|
<StackPanel Orientation="Horizontal">
|
||||||
|
<u:PaginationButton Name="{x:Static u:Pagination.PART_PreviousButton}" u:DisabledAdorner.IsEnabled="True">
|
||||||
|
<PathIcon
|
||||||
|
Width="12"
|
||||||
|
Height="12"
|
||||||
|
Data="{DynamicResource PaginationBackwardGlyph}"
|
||||||
|
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
||||||
|
</u:PaginationButton>
|
||||||
|
<Grid ColumnDefinitions="*, Auto, *">
|
||||||
|
<u:NumericIntUpDown
|
||||||
|
Name="{x:Static u:Pagination.PART_QuickJumpInput}"
|
||||||
|
Grid.Column="0"
|
||||||
|
MinWidth="{Binding $self.Bounds.Height}"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
HorizontalContentAlignment="Center"
|
||||||
|
ShowButtonSpinner="False"
|
||||||
|
IsVisible="True"
|
||||||
|
Value="{TemplateBinding CurrentPage,
|
||||||
|
Mode=TwoWay}" />
|
||||||
|
<TextBlock
|
||||||
|
Name="PART_PageTextBlock"
|
||||||
|
Grid.Column="0"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
IsVisible="False"
|
||||||
|
Text="{TemplateBinding CurrentPage}" />
|
||||||
|
<TextBlock
|
||||||
|
Grid.Column="1"
|
||||||
|
Margin="8,0"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Text="/" />
|
||||||
|
<SelectableTextBlock
|
||||||
|
Grid.Column="2"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Text="{TemplateBinding PageCount}" />
|
||||||
|
</Grid>
|
||||||
|
<u:PaginationButton Name="{x:Static u:Pagination.PART_NextButton}" u:DisabledAdorner.IsEnabled="True">
|
||||||
|
<PathIcon
|
||||||
|
Width="12"
|
||||||
|
Height="12"
|
||||||
|
Data="{DynamicResource PaginationForwardGlyph}"
|
||||||
|
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
||||||
|
</u:PaginationButton>
|
||||||
|
</StackPanel>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter>
|
||||||
|
<Style Selector="^.ReadOnly /template/ TextBlock#PART_PageTextBlock">
|
||||||
|
<Setter Property="IsVisible" Value="True" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="^.ReadOnly /template/ u|NumericUpDown#PART_QuickJumpInput">
|
||||||
|
<Setter Property="IsVisible" Value="False" />
|
||||||
|
</Style>
|
||||||
|
</ControlTheme>
|
||||||
|
|
||||||
<ControlTheme x:Key="{x:Type u:PaginationButton}" TargetType="u:PaginationButton">
|
<ControlTheme x:Key="{x:Type u:PaginationButton}" TargetType="u:PaginationButton">
|
||||||
<Setter Property="Content" Value="{Binding $self.Page}" />
|
<Setter Property="Content" Value="{Binding $self.Page}" />
|
||||||
<Setter Property="BorderBrush" Value="Transparent" />
|
<Setter Property="BorderBrush" Value="Transparent" />
|
||||||
@@ -130,4 +190,4 @@
|
|||||||
</Style>
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Windows.Input;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Collections;
|
using Avalonia.Collections;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
@@ -7,154 +8,75 @@ using Avalonia.Input;
|
|||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
using Irihi.Avalonia.Shared.Helpers;
|
using Irihi.Avalonia.Shared.Helpers;
|
||||||
using System.Windows.Input;
|
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pagination is a control that displays a series of buttons that can be used to navigate to pages.
|
/// Pagination is a control that displays a series of buttons that can be used to navigate to pages.
|
||||||
/// CurrentPage starts from 1.
|
/// CurrentPage starts from 1.
|
||||||
/// Pagination only stores an approximate index internally.
|
/// Pagination only stores an approximate index internally.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[TemplatePart(PART_PreviousButton, typeof(PaginationButton))]
|
[TemplatePart(PART_PreviousButton, typeof(PaginationButton))]
|
||||||
[TemplatePart(PART_NextButton, typeof(PaginationButton))]
|
[TemplatePart(PART_NextButton, typeof(PaginationButton))]
|
||||||
[TemplatePart(PART_ButtonPanel, typeof(StackPanel))]
|
[TemplatePart(PART_ButtonPanel, typeof(StackPanel))]
|
||||||
[TemplatePart(PART_QuickJumpInput, typeof(NumericIntUpDown))]
|
[TemplatePart(PART_QuickJumpInput, typeof(NumericIntUpDown))]
|
||||||
public class Pagination: TemplatedControl
|
public class Pagination : TemplatedControl
|
||||||
{
|
{
|
||||||
public const string PART_PreviousButton = "PART_PreviousButton";
|
public const string PART_PreviousButton = "PART_PreviousButton";
|
||||||
public const string PART_NextButton = "PART_NextButton";
|
public const string PART_NextButton = "PART_NextButton";
|
||||||
public const string PART_ButtonPanel = "PART_ButtonPanel";
|
public const string PART_ButtonPanel = "PART_ButtonPanel";
|
||||||
public const string PART_QuickJumpInput = "PART_QuickJumpInput";
|
public const string PART_QuickJumpInput = "PART_QuickJumpInput";
|
||||||
private PaginationButton? _previousButton;
|
|
||||||
private PaginationButton? _nextButton;
|
|
||||||
private StackPanel? _buttonPanel;
|
|
||||||
private readonly PaginationButton[] _buttons = new PaginationButton[7];
|
|
||||||
private NumericIntUpDown? _quickJumpInput;
|
|
||||||
|
|
||||||
public static readonly StyledProperty<int?> CurrentPageProperty = AvaloniaProperty.Register<Pagination, int?>(
|
public static readonly StyledProperty<int?> CurrentPageProperty = AvaloniaProperty.Register<Pagination, int?>(
|
||||||
nameof(CurrentPage));
|
nameof(CurrentPage), coerce: CoerceCurrentPage);
|
||||||
|
|
||||||
public int? CurrentPage
|
|
||||||
{
|
|
||||||
get => GetValue(CurrentPageProperty);
|
|
||||||
set => SetValue(CurrentPageProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnCurrentPageChanged(AvaloniaPropertyChangedEventArgs<int?> args)
|
|
||||||
{
|
|
||||||
int? oldValue = args.GetOldValue<int?>();
|
|
||||||
int? newValue = args.GetNewValue<int?>();
|
|
||||||
var e = new ValueChangedEventArgs<int>(CurrentPageChangedEvent, oldValue, newValue);
|
|
||||||
RaiseEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly RoutedEvent<ValueChangedEventArgs<int>> CurrentPageChangedEvent =
|
public static readonly RoutedEvent<ValueChangedEventArgs<int>> CurrentPageChangedEvent =
|
||||||
RoutedEvent.Register<Pagination, ValueChangedEventArgs<int>>(nameof(CurrentPageChanged), RoutingStrategies.Bubble);
|
RoutedEvent.Register<Pagination, ValueChangedEventArgs<int>>(nameof(CurrentPageChanged),
|
||||||
|
RoutingStrategies.Bubble);
|
||||||
/// <summary>
|
|
||||||
/// Raised when the <see cref="CurrentPage"/> changes.
|
|
||||||
/// </summary>
|
|
||||||
public event EventHandler<ValueChangedEventArgs<int>>? CurrentPageChanged
|
|
||||||
{
|
|
||||||
add => AddHandler(CurrentPageChangedEvent, value);
|
|
||||||
remove => RemoveHandler(CurrentPageChangedEvent, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<Pagination, ICommand?>(
|
public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<Pagination, ICommand?>(
|
||||||
nameof(Command));
|
nameof(Command));
|
||||||
|
|
||||||
public ICommand? Command
|
public static readonly StyledProperty<object?> CommandParameterProperty =
|
||||||
{
|
AvaloniaProperty.Register<Pagination, object?>(nameof(CommandParameter));
|
||||||
get => GetValue(CommandProperty);
|
|
||||||
set => SetValue(CommandProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<object?> CommandParameterProperty = AvaloniaProperty.Register<Pagination, object?>(nameof(CommandParameter));
|
|
||||||
|
|
||||||
public object? CommandParameter
|
|
||||||
{
|
|
||||||
get => this.GetValue(CommandParameterProperty);
|
|
||||||
set => this.SetValue(CommandParameterProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<int> TotalCountProperty = AvaloniaProperty.Register<Pagination, int>(
|
public static readonly StyledProperty<int> TotalCountProperty = AvaloniaProperty.Register<Pagination, int>(
|
||||||
nameof(TotalCount));
|
nameof(TotalCount));
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Total count of items.
|
|
||||||
/// </summary>
|
|
||||||
public int TotalCount
|
|
||||||
{
|
|
||||||
get => GetValue(TotalCountProperty);
|
|
||||||
set => SetValue(TotalCountProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<int> PageSizeProperty = AvaloniaProperty.Register<Pagination, int>(
|
public static readonly StyledProperty<int> PageSizeProperty = AvaloniaProperty.Register<Pagination, int>(
|
||||||
nameof(PageSize), defaultValue: 10);
|
nameof(PageSize), 10);
|
||||||
|
|
||||||
/// <summary>
|
public static readonly DirectProperty<Pagination, int> PageCountProperty =
|
||||||
/// Page size.
|
AvaloniaProperty.RegisterDirect<Pagination, int>(
|
||||||
/// </summary>
|
nameof(PageCount), o => o.PageCount, (o, e) => o.PageCount = e);
|
||||||
public int PageSize
|
|
||||||
{
|
|
||||||
get => GetValue(PageSizeProperty);
|
|
||||||
set => SetValue(PageSizeProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int _pageCount;
|
public static readonly StyledProperty<AvaloniaList<int>> PageSizeOptionsProperty =
|
||||||
|
AvaloniaProperty.Register<Pagination, AvaloniaList<int>>(
|
||||||
|
nameof(PageSizeOptions));
|
||||||
|
|
||||||
public static readonly DirectProperty<Pagination, int> PageCountProperty = AvaloniaProperty.RegisterDirect<Pagination, int>(
|
public static readonly StyledProperty<ControlTheme> PageButtonThemeProperty =
|
||||||
nameof(PageCount), o => o.PageCount);
|
AvaloniaProperty.Register<Pagination, ControlTheme>(
|
||||||
|
nameof(PageButtonTheme));
|
||||||
|
|
||||||
/// <summary>
|
public static readonly StyledProperty<bool> ShowPageSizeSelectorProperty =
|
||||||
/// Page count.
|
AvaloniaProperty.Register<Pagination, bool>(
|
||||||
/// </summary>
|
nameof(ShowPageSizeSelector));
|
||||||
public int PageCount
|
|
||||||
{
|
|
||||||
get => _pageCount;
|
|
||||||
private set => SetAndRaise(PageCountProperty, ref _pageCount, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<AvaloniaList<int>> PageSizeOptionsProperty = AvaloniaProperty.Register<Pagination, AvaloniaList<int>>(
|
|
||||||
nameof(PageSizeOptions));
|
|
||||||
|
|
||||||
public AvaloniaList<int> PageSizeOptions
|
|
||||||
{
|
|
||||||
get => GetValue(PageSizeOptionsProperty);
|
|
||||||
set => SetValue(PageSizeOptionsProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<ControlTheme> PageButtonThemeProperty = AvaloniaProperty.Register<Pagination, ControlTheme>(
|
|
||||||
nameof(PageButtonTheme));
|
|
||||||
|
|
||||||
public ControlTheme PageButtonTheme
|
|
||||||
{
|
|
||||||
get => GetValue(PageButtonThemeProperty);
|
|
||||||
set => SetValue(PageButtonThemeProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> ShowPageSizeSelectorProperty = AvaloniaProperty.Register<Pagination, bool>(
|
|
||||||
nameof(ShowPageSizeSelector));
|
|
||||||
|
|
||||||
public bool ShowPageSizeSelector
|
|
||||||
{
|
|
||||||
get => GetValue(ShowPageSizeSelectorProperty);
|
|
||||||
set => SetValue(ShowPageSizeSelectorProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> ShowQuickJumpProperty = AvaloniaProperty.Register<Pagination, bool>(
|
public static readonly StyledProperty<bool> ShowQuickJumpProperty = AvaloniaProperty.Register<Pagination, bool>(
|
||||||
nameof(ShowQuickJump));
|
nameof(ShowQuickJump));
|
||||||
|
|
||||||
public bool ShowQuickJump
|
private readonly PaginationButton[] _buttons = new PaginationButton[7];
|
||||||
{
|
private StackPanel? _buttonPanel;
|
||||||
get => GetValue(ShowQuickJumpProperty);
|
private PaginationButton? _nextButton;
|
||||||
set => SetValue(ShowQuickJumpProperty, value);
|
|
||||||
}
|
private int _pageCount;
|
||||||
|
private PaginationButton? _previousButton;
|
||||||
|
private NumericIntUpDown? _quickJumpInput;
|
||||||
|
|
||||||
static Pagination()
|
static Pagination()
|
||||||
{
|
{
|
||||||
PageSizeProperty.Changed.AddClassHandler<Pagination, int>((pagination, args) => pagination.OnPageSizeChanged(args));
|
PageSizeProperty.Changed.AddClassHandler<Pagination, int>((pagination, args) =>
|
||||||
|
pagination.OnPageSizeChanged(args));
|
||||||
CurrentPageProperty.Changed.AddClassHandler<Pagination, int?>((pagination, args) =>
|
CurrentPageProperty.Changed.AddClassHandler<Pagination, int?>((pagination, args) =>
|
||||||
pagination.UpdateButtonsByCurrentPage(args.NewValue.Value));
|
pagination.UpdateButtonsByCurrentPage(args.NewValue.Value));
|
||||||
CurrentPageProperty.Changed.AddClassHandler<Pagination, int?>((pagination, args) =>
|
CurrentPageProperty.Changed.AddClassHandler<Pagination, int?>((pagination, args) =>
|
||||||
@@ -163,19 +85,119 @@ public class Pagination: TemplatedControl
|
|||||||
pagination.UpdateButtonsByCurrentPage(pagination.CurrentPage));
|
pagination.UpdateButtonsByCurrentPage(pagination.CurrentPage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int? CurrentPage
|
||||||
|
{
|
||||||
|
get => GetValue(CurrentPageProperty);
|
||||||
|
set => SetValue(CurrentPageProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICommand? Command
|
||||||
|
{
|
||||||
|
get => GetValue(CommandProperty);
|
||||||
|
set => SetValue(CommandProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? CommandParameter
|
||||||
|
{
|
||||||
|
get => GetValue(CommandParameterProperty);
|
||||||
|
set => SetValue(CommandParameterProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total count of items.
|
||||||
|
/// </summary>
|
||||||
|
public int TotalCount
|
||||||
|
{
|
||||||
|
get => GetValue(TotalCountProperty);
|
||||||
|
set => SetValue(TotalCountProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Page size.
|
||||||
|
/// </summary>
|
||||||
|
public int PageSize
|
||||||
|
{
|
||||||
|
get => GetValue(PageSizeProperty);
|
||||||
|
set => SetValue(PageSizeProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Page count.
|
||||||
|
/// </summary>
|
||||||
|
public int PageCount
|
||||||
|
{
|
||||||
|
get => _pageCount;
|
||||||
|
private set => SetAndRaise(PageCountProperty, ref _pageCount, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AvaloniaList<int> PageSizeOptions
|
||||||
|
{
|
||||||
|
get => GetValue(PageSizeOptionsProperty);
|
||||||
|
set => SetValue(PageSizeOptionsProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ControlTheme PageButtonTheme
|
||||||
|
{
|
||||||
|
get => GetValue(PageButtonThemeProperty);
|
||||||
|
set => SetValue(PageButtonThemeProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ShowPageSizeSelector
|
||||||
|
{
|
||||||
|
get => GetValue(ShowPageSizeSelectorProperty);
|
||||||
|
set => SetValue(ShowPageSizeSelectorProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ShowQuickJump
|
||||||
|
{
|
||||||
|
get => GetValue(ShowQuickJumpProperty);
|
||||||
|
set => SetValue(ShowQuickJumpProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<bool> DisplayCurrentPageInQuickJumperProperty = AvaloniaProperty.Register<Pagination, bool>(
|
||||||
|
nameof(DisplayCurrentPageInQuickJumper));
|
||||||
|
|
||||||
|
public bool DisplayCurrentPageInQuickJumper
|
||||||
|
{
|
||||||
|
get => GetValue(DisplayCurrentPageInQuickJumperProperty);
|
||||||
|
set => SetValue(DisplayCurrentPageInQuickJumperProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int? CoerceCurrentPage(AvaloniaObject arg1, int? arg2)
|
||||||
|
{
|
||||||
|
if (arg2 is null) return null;
|
||||||
|
if (arg1 is Pagination p) arg2 = MathHelpers.SafeClamp(arg2.Value, 1, p.PageCount);
|
||||||
|
return arg2;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnCurrentPageChanged(AvaloniaPropertyChangedEventArgs<int?> args)
|
||||||
|
{
|
||||||
|
var oldValue = args.GetOldValue<int?>();
|
||||||
|
var newValue = args.GetNewValue<int?>();
|
||||||
|
var e = new ValueChangedEventArgs<int>(CurrentPageChangedEvent, oldValue, newValue);
|
||||||
|
if (DisplayCurrentPageInQuickJumper)
|
||||||
|
{
|
||||||
|
_quickJumpInput?.SetCurrentValue(NumericIntUpDown.ValueProperty, newValue);
|
||||||
|
}
|
||||||
|
RaiseEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Raised when the <see cref="CurrentPage" /> changes.
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<ValueChangedEventArgs<int>>? CurrentPageChanged
|
||||||
|
{
|
||||||
|
add => AddHandler(CurrentPageChangedEvent, value);
|
||||||
|
remove => RemoveHandler(CurrentPageChangedEvent, value);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnPageSizeChanged(AvaloniaPropertyChangedEventArgs<int> args)
|
private void OnPageSizeChanged(AvaloniaPropertyChangedEventArgs<int> args)
|
||||||
{
|
{
|
||||||
int pageCount = TotalCount / args.NewValue.Value;
|
var pageCount = TotalCount / args.NewValue.Value;
|
||||||
int residue = TotalCount % args.NewValue.Value;
|
var residue = TotalCount % args.NewValue.Value;
|
||||||
if (residue > 0)
|
if (residue > 0) pageCount++;
|
||||||
{
|
|
||||||
pageCount++;
|
|
||||||
}
|
|
||||||
PageCount = pageCount;
|
PageCount = pageCount;
|
||||||
if (CurrentPage > PageCount)
|
if (CurrentPage > PageCount) CurrentPage = null;
|
||||||
{
|
|
||||||
CurrentPage = null;
|
|
||||||
}
|
|
||||||
UpdateButtonsByCurrentPage(CurrentPage);
|
UpdateButtonsByCurrentPage(CurrentPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,10 +223,7 @@ public class Pagination: TemplatedControl
|
|||||||
|
|
||||||
private void OnQuickJumpInputKeyDown(object? sender, KeyEventArgs e)
|
private void OnQuickJumpInputKeyDown(object? sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.Key is Key.Enter or Key.Return)
|
if (e.Key is Key.Enter or Key.Return) SyncQuickJumperValue();
|
||||||
{
|
|
||||||
SyncQuickJumperValue();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnQuickJumpInputLostFocus(object? sender, RoutedEventArgs e)
|
private void OnQuickJumpInputLostFocus(object? sender, RoutedEventArgs e)
|
||||||
@@ -219,7 +238,10 @@ public class Pagination: TemplatedControl
|
|||||||
if (value is null) return;
|
if (value is null) return;
|
||||||
value = Clamp(value.Value, 1, PageCount);
|
value = Clamp(value.Value, 1, PageCount);
|
||||||
SetCurrentValue(CurrentPageProperty, value);
|
SetCurrentValue(CurrentPageProperty, value);
|
||||||
_quickJumpInput?.SetCurrentValue(NumericIntUpDown.ValueProperty, null);
|
if (!DisplayCurrentPageInQuickJumper)
|
||||||
|
{
|
||||||
|
_quickJumpInput?.SetCurrentValue(NumericIntUpDown.ValueProperty, null);
|
||||||
|
}
|
||||||
InvokeCommand();
|
InvokeCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,9 +256,9 @@ public class Pagination: TemplatedControl
|
|||||||
{
|
{
|
||||||
if (_buttonPanel is null) return;
|
if (_buttonPanel is null) return;
|
||||||
_buttonPanel.Children.Clear();
|
_buttonPanel.Children.Clear();
|
||||||
for (int i = 1; i <= 7; i++)
|
for (var i = 1; i <= 7; i++)
|
||||||
{
|
{
|
||||||
var button = new PaginationButton() { Page = i, IsVisible = true };
|
var button = new PaginationButton { Page = i, IsVisible = true };
|
||||||
_buttonPanel.Children.Add(button);
|
_buttonPanel.Children.Add(button);
|
||||||
_buttons[i - 1] = button;
|
_buttons[i - 1] = button;
|
||||||
Button.ClickEvent.AddHandler(OnPageButtonClick, button);
|
Button.ClickEvent.AddHandler(OnPageButtonClick, button);
|
||||||
@@ -248,24 +270,19 @@ public class Pagination: TemplatedControl
|
|||||||
if (sender is PaginationButton pageButton)
|
if (sender is PaginationButton pageButton)
|
||||||
{
|
{
|
||||||
if (pageButton.IsFastForward)
|
if (pageButton.IsFastForward)
|
||||||
{
|
|
||||||
AddCurrentPage(-5);
|
AddCurrentPage(-5);
|
||||||
}
|
|
||||||
else if (pageButton.IsFastBackward)
|
else if (pageButton.IsFastBackward)
|
||||||
{
|
|
||||||
AddCurrentPage(5);
|
AddCurrentPage(5);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
CurrentPage = pageButton.Page;
|
CurrentPage = pageButton.Page;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InvokeCommand();
|
InvokeCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddCurrentPage(int pageChange)
|
private void AddCurrentPage(int pageChange)
|
||||||
{
|
{
|
||||||
int newValue = (CurrentPage ?? 0) + pageChange;
|
var newValue = (CurrentPage ?? 0) + pageChange;
|
||||||
newValue = Clamp(newValue, 1, PageCount);
|
newValue = Clamp(newValue, 1, PageCount);
|
||||||
SetCurrentValue(CurrentPageProperty, newValue);
|
SetCurrentValue(CurrentPageProperty, newValue);
|
||||||
}
|
}
|
||||||
@@ -276,26 +293,26 @@ public class Pagination: TemplatedControl
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update Button Content and Visibility by current page.
|
/// Update Button Content and Visibility by current page.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="page"></param>
|
/// <param name="page"></param>
|
||||||
private void UpdateButtonsByCurrentPage(int? page)
|
private void UpdateButtonsByCurrentPage(int? page)
|
||||||
{
|
{
|
||||||
if (_buttonPanel is null) return;
|
|
||||||
if (PageSize == 0) return;
|
if (PageSize == 0) return;
|
||||||
|
var pageCount = TotalCount / PageSize;
|
||||||
int? currentPage = CurrentPage;
|
if (_buttonPanel is null)
|
||||||
int pageCount = TotalCount / PageSize;
|
|
||||||
int residue = TotalCount % PageSize;
|
|
||||||
if (residue > 0)
|
|
||||||
{
|
{
|
||||||
pageCount++;
|
SetCurrentValue(PageCountProperty, pageCount);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var currentPage = CurrentPage;
|
||||||
|
var residue = TotalCount % PageSize;
|
||||||
|
if (residue > 0) pageCount++;
|
||||||
|
|
||||||
if (pageCount <= 7)
|
if (pageCount <= 7)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 7; i++)
|
for (var i = 0; i < 7; i++)
|
||||||
{
|
|
||||||
if (i < pageCount)
|
if (i < pageCount)
|
||||||
{
|
{
|
||||||
_buttons[i].IsVisible = true;
|
_buttons[i].IsVisible = true;
|
||||||
@@ -305,15 +322,11 @@ public class Pagination: TemplatedControl
|
|||||||
{
|
{
|
||||||
_buttons[i].IsVisible = false;
|
_buttons[i].IsVisible = false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 7; i++)
|
for (var i = 0; i < 7; i++) _buttons[i].IsVisible = true;
|
||||||
{
|
var mid = currentPage ?? 0;
|
||||||
_buttons[i].IsVisible = true;
|
|
||||||
}
|
|
||||||
int mid = currentPage ?? 0;
|
|
||||||
mid = Clamp(mid, 4, pageCount - 3);
|
mid = Clamp(mid, 4, pageCount - 3);
|
||||||
_buttons[3].Page = mid;
|
_buttons[3].Page = mid;
|
||||||
_buttons[2].Page = mid - 1;
|
_buttons[2].Page = mid - 1;
|
||||||
@@ -321,45 +334,29 @@ public class Pagination: TemplatedControl
|
|||||||
_buttons[0].Page = 1;
|
_buttons[0].Page = 1;
|
||||||
_buttons[6].Page = pageCount;
|
_buttons[6].Page = pageCount;
|
||||||
if (mid > 4)
|
if (mid > 4)
|
||||||
{
|
|
||||||
_buttons[1].SetStatus(-1, false, true, false);
|
_buttons[1].SetStatus(-1, false, true, false);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
_buttons[1].SetStatus(mid - 2, false, false, false);
|
_buttons[1].SetStatus(mid - 2, false, false, false);
|
||||||
}
|
|
||||||
if (mid < pageCount - 3)
|
if (mid < pageCount - 3)
|
||||||
{
|
|
||||||
_buttons[5].SetStatus(-1, false, false, true);
|
_buttons[5].SetStatus(-1, false, false, true);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
_buttons[5].SetStatus(mid + 2, false, false, false);
|
_buttons[5].SetStatus(mid + 2, false, false, false);
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var button in _buttons)
|
foreach (var button in _buttons)
|
||||||
{
|
|
||||||
if (button.Page == currentPage)
|
if (button.Page == currentPage)
|
||||||
{
|
|
||||||
button.SetSelected(true);
|
button.SetSelected(true);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
button.SetSelected(false);
|
button.SetSelected(false);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PageCount = pageCount;
|
SetCurrentValue(PageCountProperty, pageCount);
|
||||||
SetCurrentValue(CurrentPageProperty, currentPage);
|
SetCurrentValue(CurrentPageProperty, currentPage);
|
||||||
if (_previousButton != null) _previousButton.IsEnabled = (CurrentPage ?? int.MaxValue) > 1;
|
if (_previousButton != null) _previousButton.IsEnabled = (CurrentPage ?? int.MaxValue) > 1;
|
||||||
if (_nextButton != null) _nextButton.IsEnabled = (CurrentPage ?? 0) < PageCount;
|
if (_nextButton != null) _nextButton.IsEnabled = (CurrentPage ?? 0) < PageCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InvokeCommand()
|
private void InvokeCommand()
|
||||||
{
|
{
|
||||||
if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
|
if (Command != null && Command.CanExecute(CommandParameter)) Command.Execute(CommandParameter);
|
||||||
{
|
|
||||||
this.Command.Execute(this.CommandParameter);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ using Avalonia.Controls.Metadata;
|
|||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
[PseudoClasses(PC_Left, PC_Right, PC_Selected)]
|
[PseudoClasses(PC_Left, PC_Right, PC_Selected)]
|
||||||
public class PaginationButton: Button
|
public class PaginationButton: RepeatButton
|
||||||
{
|
{
|
||||||
public const string PC_Left = ":left";
|
public const string PC_Left = ":left";
|
||||||
public const string PC_Right = ":right";
|
public const string PC_Right = ":right";
|
||||||
|
|||||||
Reference in New Issue
Block a user