From 3dab3b6571126506cdc4f4f5fff74d1aaeef3073 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 10 Oct 2018 19:20:49 +0800 Subject: [PATCH] Pagination was refactored --- HandyControl/Controls/{ => Clock}/Clock.cs | 1 + .../Controls/{ => Clock}/ClockRadioButton.cs | 5 +- .../{Pagination.xaml.cs => Pagination.cs} | 176 ++++++++++++++---- HandyControl/Controls/Pagination.xaml | 40 ---- .../Controls/{ => Picker}/ColorPicker.xaml | 0 .../Controls/{ => Picker}/ColorPicker.xaml.cs | 0 .../Controls/{ => Picker}/TimePicker.cs | 7 +- HandyControl/HandyControl.csproj | 26 +-- HandyControl/Interactivity/ControlCommands.cs | 5 + HandyControl/Properties/AssemblyInfo.cs | 4 +- HandyControl/Themes/Styles/Pagination.xaml | 51 ++++- HandyControlDemo/MainWindow.xaml.cs | 1 + HandyControlDemo/Properties/AssemblyInfo.cs | 4 +- 13 files changed, 214 insertions(+), 106 deletions(-) rename HandyControl/Controls/{ => Clock}/Clock.cs (99%) rename HandyControl/Controls/{ => Clock}/ClockRadioButton.cs (61%) rename HandyControl/Controls/{Pagination.xaml.cs => Pagination.cs} (59%) delete mode 100644 HandyControl/Controls/Pagination.xaml rename HandyControl/Controls/{ => Picker}/ColorPicker.xaml (100%) rename HandyControl/Controls/{ => Picker}/ColorPicker.xaml.cs (100%) rename HandyControl/Controls/{ => Picker}/TimePicker.cs (99%) diff --git a/HandyControl/Controls/Clock.cs b/HandyControl/Controls/Clock/Clock.cs similarity index 99% rename from HandyControl/Controls/Clock.cs rename to HandyControl/Controls/Clock/Clock.cs index b1b34cb0..ec82730a 100644 --- a/HandyControl/Controls/Clock.cs +++ b/HandyControl/Controls/Clock/Clock.cs @@ -9,6 +9,7 @@ using System.Windows.Media; using HandyControl.Data; using HandyControl.Tools; +// ReSharper disable once CheckNamespace namespace HandyControl.Controls { [TemplatePart(Name = ElementButtonAm, Type = typeof(RadioButton))] diff --git a/HandyControl/Controls/ClockRadioButton.cs b/HandyControl/Controls/Clock/ClockRadioButton.cs similarity index 61% rename from HandyControl/Controls/ClockRadioButton.cs rename to HandyControl/Controls/Clock/ClockRadioButton.cs index f6c030c1..e08a0bce 100644 --- a/HandyControl/Controls/ClockRadioButton.cs +++ b/HandyControl/Controls/Clock/ClockRadioButton.cs @@ -1,7 +1,6 @@ -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; +using System.Windows.Controls; +// ReSharper disable once CheckNamespace namespace HandyControl.Controls { public class ClockRadioButton : RadioButton diff --git a/HandyControl/Controls/Pagination.xaml.cs b/HandyControl/Controls/Pagination.cs similarity index 59% rename from HandyControl/Controls/Pagination.xaml.cs rename to HandyControl/Controls/Pagination.cs index 034defbb..a2d39f15 100644 --- a/HandyControl/Controls/Pagination.xaml.cs +++ b/HandyControl/Controls/Pagination.cs @@ -1,16 +1,53 @@ using System; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using HandyControl.Data; +using HandyControl.Interactivity; using HandyControl.Tools.Extension; namespace HandyControl.Controls { /// - /// Pagination.xaml 的交互逻辑 + /// 页码 /// - public partial class Pagination + [TemplatePart(Name = ElementButtonLeft, Type = typeof(Button))] + [TemplatePart(Name = ElementButtonRight, Type = typeof(Button))] + [TemplatePart(Name = ElementButtonFirst, Type = typeof(RadioButton))] + [TemplatePart(Name = ElementTextBlockLeft, Type = typeof(TextBlock))] + [TemplatePart(Name = ElementPanelMain, Type = typeof(Panel))] + [TemplatePart(Name = ElementTextBlockRight, Type = typeof(TextBlock))] + [TemplatePart(Name = ElementButtonLast, Type = typeof(RadioButton))] + public class Pagination : Control { + #region Constants + + private const string ElementButtonLeft = "PART_ButtonLeft"; + private const string ElementButtonRight = "PART_ButtonRight"; + private const string ElementButtonFirst = "PART_ButtonFirst"; + private const string ElementTextBlockLeft = "PART_TextBlockLeft"; + private const string ElementPanelMain = "PART_PanelMain"; + private const string ElementTextBlockRight = "PART_TextBlockRight"; + private const string ElementButtonLast = "PART_ButtonLast"; + + #endregion Constants + + #region Data + + private Button _buttonLeft; + private Button _buttonRight; + private RadioButton _buttonFirst; + private TextBlock _textBlockLeft; + private Panel _panelMain; + private TextBlock _textBlockRight; + private RadioButton _buttonLast; + + private bool _appliedTemplate; + + #endregion Data + + #region Public Events + /// /// 页面更新事件 /// @@ -18,7 +55,27 @@ namespace HandyControl.Controls EventManager.RegisterRoutedEvent("PageUpdated", RoutingStrategy.Bubble, typeof(EventHandler>), typeof(Pagination)); - public Pagination() => InitializeComponent(); + /// + /// 页面更新事件 + /// + public event EventHandler> PageUpdated + { + add => AddHandler(PageUpdatedEvent, value); + remove => RemoveHandler(PageUpdatedEvent, value); + } + + #endregion Public Events + + public Pagination() + { + CommandBindings.Add(new CommandBinding(ControlCommands.Prev, ButtonPrev_OnClick)); + CommandBindings.Add(new CommandBinding(ControlCommands.Next, ButtonNext_OnClick)); + CommandBindings.Add(new CommandBinding(ControlCommands.Selected, ToggleButton_OnChecked)); + } + + #region Public Properties + + #region MaxPageCount /// /// 最大页数 @@ -32,6 +89,7 @@ namespace HandyControl.Controls { pagination.PageIndex = pagination.MaxPageCount; } + pagination.Show(value > 1); pagination.Update(); } @@ -55,13 +113,17 @@ namespace HandyControl.Controls set => SetValue(MaxPageCountProperty, value); } + #endregion MaxPageCount + + #region DataCountPerPage + /// /// 每页的数据量 /// public static readonly DependencyProperty DataCountPerPageProperty = DependencyProperty.Register( "DataCountPerPage", typeof(int), typeof(Pagination), new PropertyMetadata(20, (o, args) => { - if (o is Pagination pagination && args.NewValue is int) + if (o is Pagination pagination) { pagination.Update(); } @@ -85,6 +147,10 @@ namespace HandyControl.Controls set => SetValue(DataCountPerPageProperty, value); } + #endregion + + #region PageIndex + /// /// 当前页 /// @@ -120,6 +186,10 @@ namespace HandyControl.Controls set => SetValue(PageIndexProperty, value); } + #endregion PageIndex + + #region MaxPageInterval + /// /// 表示当前选中的按钮距离左右两个方向按钮的最大间隔(4表示间隔4个按钮,如果超过则用省略号表示) /// @@ -145,13 +215,40 @@ namespace HandyControl.Controls set => SetValue(MaxPageIntervalProperty, value); } - /// - /// 页面更新事件 - /// - public event EventHandler> PageUpdated + #endregion MaxPageInterval + + #endregion + + #region Public Methods + + public override void OnApplyTemplate() { - add => AddHandler(PageUpdatedEvent, value); - remove => RemoveHandler(PageUpdatedEvent, value); + _appliedTemplate = false; + base.OnApplyTemplate(); + + _buttonLeft = GetTemplateChild(ElementButtonLeft) as Button; + _buttonRight = GetTemplateChild(ElementButtonRight) as Button; + _buttonFirst = GetTemplateChild(ElementButtonFirst) as RadioButton; + _textBlockLeft = GetTemplateChild(ElementTextBlockLeft) as TextBlock; + _panelMain = GetTemplateChild(ElementPanelMain) as Panel; + _textBlockRight = GetTemplateChild(ElementTextBlockRight) as TextBlock; + _buttonLast = GetTemplateChild(ElementButtonLast) as RadioButton; + + CheckNull(); + + _appliedTemplate = true; + Update(); + } + + #endregion Public Methods + + #region Private Methods + + private void CheckNull() + { + if (_buttonLeft == null || _buttonRight == null || _buttonFirst == null || + _textBlockLeft == null || _panelMain == null || _textBlockRight == null || + _buttonLast == null) throw new Exception(); } /// @@ -159,57 +256,58 @@ namespace HandyControl.Controls /// private void Update() { - LeftButton.IsEnabled = PageIndex > 1; - RightButton.IsEnabled = PageIndex < MaxPageCount; + if (!_appliedTemplate) return; + _buttonLeft.IsEnabled = PageIndex > 1; + _buttonRight.IsEnabled = PageIndex < MaxPageCount; if (MaxPageInterval == 0) { - FirstButton.Visibility = Visibility.Collapsed; - LastButton.Visibility = Visibility.Collapsed; - LeftTextBlock.Visibility = Visibility.Collapsed; - RightTextBlock.Visibility = Visibility.Collapsed; - MainPanel.Children.Clear(); + _buttonFirst.Visibility = Visibility.Collapsed; + _buttonLast.Visibility = Visibility.Collapsed; + _textBlockLeft.Visibility = Visibility.Collapsed; + _textBlockRight.Visibility = Visibility.Collapsed; + _panelMain.Children.Clear(); var selectButton = CreateButton(PageIndex); - MainPanel.Children.Add(selectButton); + _panelMain.Children.Add(selectButton); selectButton.IsChecked = true; return; } - FirstButton.Visibility = Visibility.Visible; - LastButton.Visibility = Visibility.Visible; - LeftTextBlock.Visibility = Visibility.Visible; - RightTextBlock.Visibility = Visibility.Visible; + _buttonFirst.Visibility = Visibility.Visible; + _buttonLast.Visibility = Visibility.Visible; + _textBlockLeft.Visibility = Visibility.Visible; + _textBlockRight.Visibility = Visibility.Visible; //更新最后一页 if (MaxPageCount == 1) { - LastButton.Visibility = Visibility.Collapsed; + _buttonLast.Visibility = Visibility.Collapsed; } else { - LastButton.Visibility = Visibility.Visible; - LastButton.Tag = MaxPageCount.ToString(); + _buttonLast.Visibility = Visibility.Visible; + _buttonLast.Tag = MaxPageCount.ToString(); } //更新省略号 var right = MaxPageCount - PageIndex; var left = PageIndex - 1; - RightTextBlock.Visibility = right > MaxPageInterval ? Visibility.Visible : Visibility.Collapsed; - LeftTextBlock.Visibility = left > MaxPageInterval ? Visibility.Visible : Visibility.Collapsed; + _textBlockRight.Visibility = right > MaxPageInterval ? Visibility.Visible : Visibility.Collapsed; + _textBlockLeft.Visibility = left > MaxPageInterval ? Visibility.Visible : Visibility.Collapsed; //更新中间部分 - MainPanel.Children.Clear(); + _panelMain.Children.Clear(); if (PageIndex > 1 && PageIndex < MaxPageCount) { var selectButton = CreateButton(PageIndex); - MainPanel.Children.Add(selectButton); + _panelMain.Children.Add(selectButton); selectButton.IsChecked = true; } else if (PageIndex == 1) { - FirstButton.IsChecked = true; + _buttonFirst.IsChecked = true; } else { - LastButton.IsChecked = true; + _buttonLast.IsChecked = true; } var sub = PageIndex; @@ -217,7 +315,7 @@ namespace HandyControl.Controls { if (--sub > 1) { - MainPanel.Children.Insert(0, CreateButton(sub)); + _panelMain.Children.Insert(0, CreateButton(sub)); } else { @@ -229,7 +327,7 @@ namespace HandyControl.Controls { if (++add < MaxPageCount) { - MainPanel.Children.Add(CreateButton(add)); + _panelMain.Children.Add(CreateButton(add)); } else { @@ -238,15 +336,9 @@ namespace HandyControl.Controls } } - private void ButtonPrev_OnClick(object sender, RoutedEventArgs e) - { - PageIndex--; - } + private void ButtonPrev_OnClick(object sender, RoutedEventArgs e) => PageIndex--; - private void ButtonNext_OnClick(object sender, RoutedEventArgs e) - { - PageIndex++; - } + private void ButtonNext_OnClick(object sender, RoutedEventArgs e) => PageIndex++; private RadioButton CreateButton(int page) { @@ -262,5 +354,7 @@ namespace HandyControl.Controls if (!(e.OriginalSource is RadioButton button)) return; PageIndex = int.Parse(button.Tag.ToString()); } + + #endregion Private Methods } } diff --git a/HandyControl/Controls/Pagination.xaml b/HandyControl/Controls/Pagination.xaml deleted file mode 100644 index c710071e..00000000 --- a/HandyControl/Controls/Pagination.xaml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/HandyControl/Controls/ColorPicker.xaml b/HandyControl/Controls/Picker/ColorPicker.xaml similarity index 100% rename from HandyControl/Controls/ColorPicker.xaml rename to HandyControl/Controls/Picker/ColorPicker.xaml diff --git a/HandyControl/Controls/ColorPicker.xaml.cs b/HandyControl/Controls/Picker/ColorPicker.xaml.cs similarity index 100% rename from HandyControl/Controls/ColorPicker.xaml.cs rename to HandyControl/Controls/Picker/ColorPicker.xaml.cs diff --git a/HandyControl/Controls/TimePicker.cs b/HandyControl/Controls/Picker/TimePicker.cs similarity index 99% rename from HandyControl/Controls/TimePicker.cs rename to HandyControl/Controls/Picker/TimePicker.cs index d1781a80..ab5be77f 100644 --- a/HandyControl/Controls/TimePicker.cs +++ b/HandyControl/Controls/Picker/TimePicker.cs @@ -8,8 +8,12 @@ using System.Windows.Input; using System.Windows.Threading; using HandyControl.Data; +// ReSharper disable once CheckNamespace namespace HandyControl.Controls { + /// + /// 时间选择器 + /// [TemplatePart(Name = ElementRoot, Type = typeof(Grid))] [TemplatePart(Name = ElementTextBox, Type = typeof(WatermarkTextBox))] [TemplatePart(Name = ElementButton, Type = typeof(Button))] @@ -356,7 +360,8 @@ namespace HandyControl.Controls private void CheckNull() { - if (_clock == null || _dropDownButton == null || _popUp == null || _textBox == null) throw new Exception(); + if (_clock == null || _dropDownButton == null || _popUp == null || _textBox == null) + throw new Exception(); } private void TextBox_LostFocus(object sender, RoutedEventArgs e) diff --git a/HandyControl/HandyControl.csproj b/HandyControl/HandyControl.csproj index 74d27940..a5d7af25 100644 --- a/HandyControl/HandyControl.csproj +++ b/HandyControl/HandyControl.csproj @@ -46,9 +46,10 @@ Carousel.xaml - - - + + + + ColorPicker.xaml @@ -67,9 +68,6 @@ - - Pagination.xaml - StepBar.xaml @@ -83,7 +81,7 @@ TimeBar.xaml - + @@ -175,7 +173,7 @@ Designer MSBuild:Compile - + Designer MSBuild:Compile @@ -191,10 +189,6 @@ Designer MSBuild:Compile - - Designer - MSBuild:Compile - Designer MSBuild:Compile @@ -419,14 +413,14 @@ Designer MSBuild:Compile - - Designer - MSBuild:Compile - Designer MSBuild:Compile + + MSBuild:Compile + Designer + diff --git a/HandyControl/Interactivity/ControlCommands.cs b/HandyControl/Interactivity/ControlCommands.cs index e2d8647a..c65e3e05 100644 --- a/HandyControl/Interactivity/ControlCommands.cs +++ b/HandyControl/Interactivity/ControlCommands.cs @@ -7,6 +7,11 @@ namespace HandyControl.Interactivity /// public static class ControlCommands { + /// + /// 选中 + /// + public static RoutedCommand Selected { get; } = new RoutedCommand("Selected", typeof(ControlCommands)); + /// /// 关闭 /// diff --git a/HandyControl/Properties/AssemblyInfo.cs b/HandyControl/Properties/AssemblyInfo.cs index ce35ea91..d90f6964 100644 --- a/HandyControl/Properties/AssemblyInfo.cs +++ b/HandyControl/Properties/AssemblyInfo.cs @@ -10,5 +10,5 @@ using System.Runtime.InteropServices; [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.3.2")] -[assembly: AssemblyFileVersion("1.3.2")] +[assembly: AssemblyVersion("1.3.2.1")] +[assembly: AssemblyFileVersion("1.3.2.1")] diff --git a/HandyControl/Themes/Styles/Pagination.xaml b/HandyControl/Themes/Styles/Pagination.xaml index 60be8c5b..8ed41e58 100644 --- a/HandyControl/Themes/Styles/Pagination.xaml +++ b/HandyControl/Themes/Styles/Pagination.xaml @@ -1,5 +1,12 @@  + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:controls="clr-namespace:HandyControl.Controls" + xmlns:interactivity="clr-namespace:HandyControl.Interactivity"> + + + + + + + \ No newline at end of file diff --git a/HandyControlDemo/MainWindow.xaml.cs b/HandyControlDemo/MainWindow.xaml.cs index 763b8abb..c6c6e147 100644 --- a/HandyControlDemo/MainWindow.xaml.cs +++ b/HandyControlDemo/MainWindow.xaml.cs @@ -9,6 +9,7 @@ namespace HandyControlDemo { public MainWindow() { + InitializeComponent(); Growl.SetGrowlPanel(PanelMessage); diff --git a/HandyControlDemo/Properties/AssemblyInfo.cs b/HandyControlDemo/Properties/AssemblyInfo.cs index 6edc6c11..b61d582b 100644 --- a/HandyControlDemo/Properties/AssemblyInfo.cs +++ b/HandyControlDemo/Properties/AssemblyInfo.cs @@ -12,5 +12,5 @@ using System.Windows; [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)] -[assembly: AssemblyVersion("1.3.2")] -[assembly: AssemblyFileVersion("1.3.2")] +[assembly: AssemblyVersion("1.3.2.1")] +[assembly: AssemblyFileVersion("1.3.2.1")]