From 6dd5d4c5ff43e790297cdfff5cd5a2c1b82460d9 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Fri, 12 Mar 2021 00:00:33 +0800 Subject: [PATCH] close #663 --- .../Controls/Other/Pagination.cs | 147 ++++++++++-------- 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Other/Pagination.cs b/src/Shared/HandyControl_Shared/Controls/Other/Pagination.cs index 0c295d3f..6bbc4dbb 100644 --- a/src/Shared/HandyControl_Shared/Controls/Other/Pagination.cs +++ b/src/Shared/HandyControl_Shared/Controls/Other/Pagination.cs @@ -76,7 +76,9 @@ namespace HandyControl.Controls CommandBindings.Add(new CommandBinding(ControlCommands.Next, ButtonNext_OnClick)); CommandBindings.Add(new CommandBinding(ControlCommands.Selected, ToggleButton_OnChecked)); CommandBindings.Add(new CommandBinding(ControlCommands.Jump, (s, e) => PageIndex = (int) _jumpNumericUpDown.Value)); - this.Show(MaxPageCount > 1); + + OnAutoHidingChanged(AutoHiding); + Update(); } #region Public Properties @@ -87,28 +89,28 @@ namespace HandyControl.Controls /// 最大页数 /// public static readonly DependencyProperty MaxPageCountProperty = DependencyProperty.Register( - "MaxPageCount", typeof(int), typeof(Pagination), new PropertyMetadata(ValueBoxes.Int1Box, (o, args) => - { - if (o is Pagination pagination && args.NewValue is int value) - { - if (pagination.PageIndex > pagination.MaxPageCount) - { - pagination.PageIndex = pagination.MaxPageCount; - } + "MaxPageCount", typeof(int), typeof(Pagination), new PropertyMetadata(ValueBoxes.Int1Box, OnMaxPageCountChanged, CoerceMaxPageCount), ValidateHelper.IsInRangeOfPosInt); - pagination.Show(value > 1); - pagination.Update(); - } - }, (o, value) => + private static object CoerceMaxPageCount(DependencyObject d, object basevalue) + { + var intValue = (int) basevalue; + return intValue < 1 ? 1 : intValue; + } + + private static void OnMaxPageCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Pagination pagination) { - if (!(o is Pagination)) return 1; - var intValue = (int) value; - if (intValue < 1) + if (pagination.PageIndex > pagination.MaxPageCount) { - return 1; + pagination.PageIndex = pagination.MaxPageCount; } - return intValue; - })); + + pagination.CoerceValue(PageIndexProperty); + pagination.OnAutoHidingChanged(pagination.AutoHiding); + pagination.Update(); + } + } /// /// 最大页数 @@ -127,22 +129,16 @@ namespace HandyControl.Controls /// 每页的数据量 /// public static readonly DependencyProperty DataCountPerPageProperty = DependencyProperty.Register( - "DataCountPerPage", typeof(int), typeof(Pagination), new PropertyMetadata(20, (o, args) => + "DataCountPerPage", typeof(int), typeof(Pagination), new PropertyMetadata(20, OnDataCountPerPageChanged), + ValidateHelper.IsInRangeOfPosInt); + + private static void OnDataCountPerPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Pagination pagination) { - if (o is Pagination pagination) - { - pagination.Update(); - } - }, (o, value) => - { - if (!(o is Pagination)) return 1; - var intValue = (int) value; - if (intValue < 1) - { - return 1; - } - return intValue; - })); + pagination.Update(); + } + } /// /// 每页的数据量 @@ -161,27 +157,31 @@ namespace HandyControl.Controls /// 当前页 /// public static readonly DependencyProperty PageIndexProperty = DependencyProperty.Register( - "PageIndex", typeof(int), typeof(Pagination), new PropertyMetadata(ValueBoxes.Int1Box, (o, args) => + "PageIndex", typeof(int), typeof(Pagination), new PropertyMetadata(ValueBoxes.Int1Box, OnPageIndexChanged, CoercePageIndex), ValidateHelper.IsInRangeOfPosInt); + + private static object CoercePageIndex(DependencyObject d, object basevalue) + { + if (!(d is Pagination pagination)) return 1; + + var intValue = (int) basevalue; + return intValue < 1 + ? 1 + : intValue > pagination.MaxPageCount + ? pagination.MaxPageCount + : intValue; + } + + private static void OnPageIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Pagination pagination && e.NewValue is int value) { - if (o is Pagination pagination && args.NewValue is int value) + pagination.Update(); + pagination.RaiseEvent(new FunctionEventArgs(PageUpdatedEvent, pagination) { - pagination.Update(); - pagination.RaiseEvent(new FunctionEventArgs(PageUpdatedEvent, pagination) - { - Info = value - }); - } - }, (o, value) => - { - if (!(o is Pagination pagination)) return 1; - var intValue = (int) value; - if (intValue < 0) - { - return 0; - } - if (intValue > pagination.MaxPageCount) return pagination.MaxPageCount; - return intValue; - })); + Info = value + }); + } + } /// /// 当前页 @@ -200,17 +200,15 @@ namespace HandyControl.Controls /// 表示当前选中的按钮距离左右两个方向按钮的最大间隔(4表示间隔4个按钮,如果超过则用省略号表示) /// public static readonly DependencyProperty MaxPageIntervalProperty = DependencyProperty.Register( - "MaxPageInterval", typeof(int), typeof(Pagination), new PropertyMetadata(3, (o, args) => + "MaxPageInterval", typeof(int), typeof(Pagination), new PropertyMetadata(3, OnMaxPageIntervalChanged), ValidateHelper.IsInRangeOfPosIntIncludeZero); + + private static void OnMaxPageIntervalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Pagination pagination) { - if (o is Pagination pagination) - { - pagination.Update(); - } - }), value => - { - var intValue = (int) value; - return intValue >= 0; - }); + pagination.Update(); + } + } /// /// 表示当前选中的按钮距离左右两个方向按钮的最大间隔(4表示间隔4个按钮,如果超过则用省略号表示) @@ -236,6 +234,29 @@ namespace HandyControl.Controls #endregion + #region AutoHiding + + public static readonly DependencyProperty AutoHidingProperty = DependencyProperty.Register( + "AutoHiding", typeof(bool), typeof(Pagination), new PropertyMetadata(ValueBoxes.TrueBox, OnAutoHidingChanged)); + + private static void OnAutoHidingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Pagination pagination) + { + pagination.OnAutoHidingChanged((bool)e.NewValue); + } + } + + private void OnAutoHidingChanged(bool newValue) => this.Show(!newValue || MaxPageCount > 1); + + public bool AutoHiding + { + get => (bool) GetValue(AutoHidingProperty); + set => SetValue(AutoHidingProperty, ValueBoxes.BooleanBox(value)); + } + + #endregion + #endregion #region Public Methods