feat: add quick jump.
This commit is contained in:
@@ -13,11 +13,16 @@
|
||||
<viewModels:PaginationDemoViewModel />
|
||||
</Design.DataContext>
|
||||
<StackPanel>
|
||||
<TextBlock Text="{Binding #page.CurrentPage}" />
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Current Page: "></TextBlock>
|
||||
<TextBlock Text="{Binding #page.CurrentPage}" />
|
||||
</StackPanel>
|
||||
<ToggleSwitch Name="pageSizeSelector" Content="Show Page Size Selector" />
|
||||
<ToggleSwitch Name="quickJumperSelector" Content="Show Quick Jumper"></ToggleSwitch>
|
||||
<u:Pagination
|
||||
Name="page"
|
||||
PageSizeOptions="10, 20, 50, 100"
|
||||
ShowQuickJump="{Binding #quickJumperSelector.IsChecked}"
|
||||
ShowPageSizeSelector="{Binding #pageSizeSelector.IsChecked}"
|
||||
TotalCount="600" />
|
||||
</StackPanel>
|
||||
|
||||
@@ -34,7 +34,7 @@ public class MenuViewModel: ViewModelBase
|
||||
// new() { MenuHeader = "Number Displayer", Key = MenuKeys.MenuKeyNumberDisplayer, Status = "New" },
|
||||
new() { MenuHeader = "Numeric UpDown", Key = MenuKeys.MenuKeyNumericUpDown },
|
||||
new() { MenuHeader = "NumPad", Key = MenuKeys.MenuKeyNumPad, Status = "New" },
|
||||
new() { MenuHeader = "Pagination", Key = MenuKeys.MenuKeyPagination },
|
||||
new() { MenuHeader = "Pagination", Key = MenuKeys.MenuKeyPagination, Status = "Updated" },
|
||||
new() { MenuHeader = "RangeSlider", Key = MenuKeys.MenuKeyRangeSlider },
|
||||
new() { MenuHeader = "Scroll To", Key = MenuKeys.MenuKeyScrollToButton, Status = "New" },
|
||||
new() { MenuHeader = "Selection List", Key = MenuKeys.MenuKeySelectionList, Status = "New" },
|
||||
|
||||
@@ -27,8 +27,18 @@
|
||||
Data="{DynamicResource PaginationForwardGlyph}"
|
||||
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
||||
</u:PaginationButton>
|
||||
<StackPanel Orientation="Horizontal" IsVisible="{TemplateBinding ShowQuickJump}">
|
||||
<TextBlock
|
||||
Margin="4 0"
|
||||
VerticalAlignment="Center"
|
||||
Text="{DynamicResource STRING_PAGINATION_JUMP_TO}" />
|
||||
<u:NumericIntUpDown x:Name="{x:Static u:Pagination.PART_QuickJumpInput}" ShowButtonSpinner="False" Width="50"></u:NumericIntUpDown>
|
||||
<TextBlock
|
||||
Margin="4 0"
|
||||
VerticalAlignment="Center"
|
||||
Text="{DynamicResource STRING_PAGINATION_PAGE}" />
|
||||
</StackPanel>
|
||||
<ComboBox
|
||||
Name="{x:Static u:Pagination.PART_SizeChangerComboBox}"
|
||||
IsVisible="{TemplateBinding ShowPageSizeSelector}"
|
||||
ItemsSource="{TemplateBinding PageSizeOptions}"
|
||||
SelectedItem="{TemplateBinding PageSize,
|
||||
|
||||
@@ -10,4 +10,6 @@
|
||||
<x:String x:Key="STRING_MENU_DIALOG_YES">Yes</x:String>
|
||||
<x:String x:Key="STRING_MENU_DIALOG_NO">No</x:String>
|
||||
<x:String x:Key="STRING_MENU_DIALOG_CLOSE">Close</x:String>
|
||||
<x:String x:Key="STRING_PAGINATION_JUMP_TO">Jump to page</x:String>
|
||||
<x:String x:Key="STRING_PAGINATION_PAGE"> </x:String>
|
||||
</ResourceDictionary>
|
||||
|
||||
@@ -10,4 +10,6 @@
|
||||
<x:String x:Key="STRING_MENU_DIALOG_YES">是</x:String>
|
||||
<x:String x:Key="STRING_MENU_DIALOG_NO">否</x:String>
|
||||
<x:String x:Key="STRING_MENU_DIALOG_CLOSE">关闭</x:String>
|
||||
<x:String x:Key="STRING_PAGINATION_JUMP_TO">跳至</x:String>
|
||||
<x:String x:Key="STRING_PAGINATION_PAGE">页</x:String>
|
||||
</ResourceDictionary>
|
||||
|
||||
@@ -3,9 +3,9 @@ using Avalonia.Collections;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Styling;
|
||||
using Avalonia.Utilities;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
@@ -18,18 +18,18 @@ namespace Ursa.Controls;
|
||||
[TemplatePart(PART_PreviousButton, typeof(PaginationButton))]
|
||||
[TemplatePart(PART_NextButton, typeof(PaginationButton))]
|
||||
[TemplatePart(PART_ButtonPanel, typeof(StackPanel))]
|
||||
[TemplatePart(PART_SizeChangerComboBox, typeof(ComboBox))]
|
||||
[TemplatePart(PART_QuickJumpInput, typeof(NumericIntUpDown))]
|
||||
public class Pagination: TemplatedControl
|
||||
{
|
||||
public const string PART_PreviousButton = "PART_PreviousButton";
|
||||
public const string PART_NextButton = "PART_NextButton";
|
||||
public const string PART_ButtonPanel = "PART_ButtonPanel";
|
||||
public const string PART_SizeChangerComboBox = "PART_SizeChangerComboBox";
|
||||
public const string PART_QuickJumpInput = "PART_QuickJumpInput";
|
||||
private PaginationButton? _previousButton;
|
||||
private PaginationButton? _nextButton;
|
||||
private StackPanel? _buttonPanel;
|
||||
private readonly PaginationButton[] _buttons = new PaginationButton[7];
|
||||
private ComboBox? _sizeChangerComboBox;
|
||||
private NumericIntUpDown? _quickJumpInput;
|
||||
|
||||
public static readonly StyledProperty<int?> CurrentPageProperty = AvaloniaProperty.Register<Pagination, int?>(
|
||||
nameof(CurrentPage));
|
||||
@@ -107,10 +107,7 @@ public class Pagination: TemplatedControl
|
||||
|
||||
public static readonly StyledProperty<bool> ShowQuickJumpProperty = AvaloniaProperty.Register<Pagination, bool>(
|
||||
nameof(ShowQuickJump));
|
||||
|
||||
/// <summary>
|
||||
/// This feature is not implemented yet.
|
||||
/// </summary>
|
||||
|
||||
public bool ShowQuickJump
|
||||
{
|
||||
get => GetValue(ShowQuickJumpProperty);
|
||||
@@ -145,16 +142,46 @@ public class Pagination: TemplatedControl
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
Button.ClickEvent.AddHandler(OnButtonClick, _previousButton, _nextButton);
|
||||
_previousButton = e.NameScope.Find<PaginationButton>(PART_PreviousButton);
|
||||
_nextButton = e.NameScope.Find<PaginationButton>(PART_NextButton);
|
||||
_buttonPanel = e.NameScope.Find<StackPanel>(PART_ButtonPanel);
|
||||
_sizeChangerComboBox = e.NameScope.Find<ComboBox>(PART_SizeChangerComboBox);
|
||||
Button.ClickEvent.AddHandler(OnButtonClick, _previousButton, _nextButton);
|
||||
|
||||
KeyDownEvent.RemoveHandler(OnQuickJumpInputKeyDown, _quickJumpInput);
|
||||
LostFocusEvent.RemoveHandler(OnQuickJumpInputLostFocus, _quickJumpInput);
|
||||
_quickJumpInput = e.NameScope.Find<NumericIntUpDown>(PART_QuickJumpInput);
|
||||
KeyDownEvent.AddHandler(OnQuickJumpInputKeyDown, _quickJumpInput);
|
||||
LostFocusEvent.AddHandler(OnQuickJumpInputLostFocus, _quickJumpInput);
|
||||
|
||||
InitializePanelButtons();
|
||||
UpdateButtonsByCurrentPage(0);
|
||||
}
|
||||
|
||||
private void OnQuickJumpInputKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key is Key.Enter or Key.Return)
|
||||
{
|
||||
SyncQuickJumperValue();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnQuickJumpInputLostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SyncQuickJumperValue();
|
||||
}
|
||||
|
||||
private void SyncQuickJumperValue()
|
||||
{
|
||||
if (_quickJumpInput is null) return;
|
||||
var value = _quickJumpInput?.Value;
|
||||
if (value is null) return;
|
||||
value = Clamp(value.Value, 1, PageCount);
|
||||
SetCurrentValue(CurrentPageProperty, value);
|
||||
_quickJumpInput?.SetCurrentValue(NumericIntUpDown.ValueProperty, null);
|
||||
}
|
||||
|
||||
private void OnButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var diff = Equals(sender, _previousButton) ? -1 : 1;
|
||||
|
||||
Reference in New Issue
Block a user