Pagination was refactored

This commit is contained in:
NaBian committed 2018-10-10 19:20:49 +08:00
1 parent ac3e41e9e6
commit 3dab3b6571
13 files changed
+214 -106

No files matched your search

@@ -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))]
@@ -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
@@ -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
{
/// <summary>
/// Pagination.xaml 的交互逻辑
/// 页码
/// </summary>
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
/// <summary>
/// 页面更新事件
/// </summary>
@@ -18,7 +55,27 @@ namespace HandyControl.Controls
EventManager.RegisterRoutedEvent("PageUpdated", RoutingStrategy.Bubble,
typeof(EventHandler<FunctionEventArgs<int>>), typeof(Pagination));
public Pagination() => InitializeComponent();
/// <summary>
/// 页面更新事件
/// </summary>
public event EventHandler<FunctionEventArgs<int>> 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
/// <summary>
/// 最大页数
@@ -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
/// <summary>
/// 每页的数据量
/// </summary>
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
/// <summary>
/// 当前页
/// </summary>
@@ -120,6 +186,10 @@ namespace HandyControl.Controls
set => SetValue(PageIndexProperty, value);
}
#endregion PageIndex
#region MaxPageInterval
/// <summary>
/// 表示当前选中的按钮距离左右两个方向按钮的最大间隔(4表示间隔4个按钮,如果超过则用省略号表示)
/// </summary>
@@ -145,13 +215,40 @@ namespace HandyControl.Controls
set => SetValue(MaxPageIntervalProperty, value);
}
/// <summary>
/// 页面更新事件
/// </summary>
public event EventHandler<FunctionEventArgs<int>> 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();
}
/// <summary>
@@ -159,57 +256,58 @@ namespace HandyControl.Controls
/// </summary>
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
}
}
-40
View File
@@ -1,40 +0,0 @@
<UserControl x:Class="HandyControl.Controls.Pagination"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="40"
Visibility="Collapsed"
UseLayoutRounding="True"
Name="MyUserControl"
Background="Transparent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!--向左切换-->
<Button Name="LeftButton" Click="ButtonPrev_OnClick" Margin="5,0" VerticalAlignment="Center" Background="{x:Null}" Style="{StaticResource ButtonCustom}">
<Border CornerRadius="2" BorderThickness="1" BorderBrush="{StaticResource BorderBrush}" Width="30" Height="30" Background="White">
<Path Margin="10" Fill="{StaticResource PrimaryTextBrush}" Style="{StaticResource LeftPathStyle}"/>
</Border>
</Button>
<StackPanel HorizontalAlignment="Center" Grid.Column="1" RadioButton.Checked="ToggleButton_OnChecked" Orientation="Horizontal">
<RadioButton Name="FirstButton" Tag="1" IsChecked="True" Style="{StaticResource PaginationButtonStyle}"/>
<TextBlock Name="LeftTextBlock" Margin="10,0" FontSize="16" Text="..." Foreground="{DynamicResource PrimaryTextBrush}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<StackPanel Name="MainPanel" Orientation="Horizontal"/>
<TextBlock Name="RightTextBlock" Margin="10,0" FontSize="16" Text="..." Foreground="{DynamicResource PrimaryTextBrush}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<RadioButton Name="LastButton" Tag="10" Style="{StaticResource PaginationButtonStyle}"/>
</StackPanel>
<!--向右切换-->
<Button Grid.Column="2" Name="RightButton" Click="ButtonNext_OnClick" Margin="5,0" VerticalAlignment="Center" Background="{x:Null}" Style="{StaticResource ButtonCustom}">
<Border BorderThickness="1" BorderBrush="{StaticResource BorderBrush}" CornerRadius="2" Width="30" Height="30" Background="White">
<Path Margin="10" Fill="{StaticResource PrimaryTextBrush}" Style="{StaticResource RightPathStyle}" RenderTransformOrigin="0.5,0.5"/>
</Border>
</Button>
</Grid>
</UserControl>
@@ -8,8 +8,12 @@ using System.Windows.Input;
using System.Windows.Threading;
using HandyControl.Data;
// ReSharper disable once CheckNamespace
namespace HandyControl.Controls
{
/// <summary>
/// 时间选择器
/// </summary>
[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)
+10 -16
View File
@@ -46,9 +46,10 @@
<DependentUpon>Carousel.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\CirclePanel.cs" />
<Compile Include="Controls\Clock.cs" />
<Compile Include="Controls\ClockRadioButton.cs" />
<Compile Include="Controls\ColorPicker.xaml.cs">
<Compile Include="Controls\Clock\Clock.cs" />
<Compile Include="Controls\Clock\ClockRadioButton.cs" />
<Compile Include="Controls\Pagination.cs" />
<Compile Include="Controls\Picker\ColorPicker.xaml.cs">
<DependentUpon>ColorPicker.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\Attach\IconElement.cs" />
@@ -67,9 +68,6 @@
<Compile Include="Controls\Loading\LoadingBase.cs" />
<Compile Include="Controls\Loading\LoadingCircle.cs" />
<Compile Include="Controls\Loading\LoadingLine.cs" />
<Compile Include="Controls\Pagination.xaml.cs">
<DependentUpon>Pagination.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\Steps\StepBar.xaml.cs">
<DependentUpon>StepBar.xaml</DependentUpon>
</Compile>
@@ -83,7 +81,7 @@
<Compile Include="Controls\TimeBar\TimeBar.xaml.cs">
<DependentUpon>TimeBar.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\TimePicker.cs" />
<Compile Include="Controls\Picker\TimePicker.cs" />
<Compile Include="Controls\TransitioningContentControl.cs" />
<Compile Include="Controls\WatermarkTextBox.cs" />
<Compile Include="Controls\Window\ImageBrowser.xaml.cs">
@@ -175,7 +173,7 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\ColorPicker.xaml">
<Page Include="Controls\Picker\ColorPicker.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
@@ -191,10 +189,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\Pagination.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\Steps\StepBar.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -419,14 +413,14 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\ThemesDefault.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\ToggleButton.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\ThemesDefault.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
@@ -7,6 +7,11 @@ namespace HandyControl.Interactivity
/// </summary>
public static class ControlCommands
{
/// <summary>
/// 选中
/// </summary>
public static RoutedCommand Selected { get; } = new RoutedCommand("Selected", typeof(ControlCommands));
/// <summary>
/// 关闭
/// </summary>
+2 -2
View File
@@ -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")]
+50 -1
View File
@@ -1,5 +1,12 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:HandyControl.Controls"
xmlns:interactivity="clr-namespace:HandyControl.Interactivity">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Basic/Paths.xaml"/>
<ResourceDictionary Source="Button.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="PaginationButtonStyle" TargetType="{x:Type RadioButton}">
<Setter Property="BorderThickness" Value="1"/>
@@ -48,4 +55,46 @@
</Style.Triggers>
</Style>
<Style TargetType="controls:Pagination">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Pagination">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!--向左切换-->
<Button x:Name="PART_ButtonLeft" Command="interactivity:ControlCommands.Prev" Margin="5,0" VerticalAlignment="Center" Style="{StaticResource ButtonCustom}">
<Border CornerRadius="2" BorderThickness="1" BorderBrush="{DynamicResource BorderBrush}" Width="30" Height="30" Background="White">
<Path Margin="10" Fill="{DynamicResource PrimaryTextBrush}" Style="{StaticResource LeftPathStyle}"/>
</Border>
</Button>
<StackPanel HorizontalAlignment="Center" Grid.Column="1" Orientation="Horizontal">
<interactivity:Interaction.Triggers>
<interactivity:RoutedEventTrigger RoutedEvent="RadioButton.Checked">
<interactivity:EventToCommand Command="interactivity:ControlCommands.Selected" PassEventArgsToCommand="True" />
</interactivity:RoutedEventTrigger>
</interactivity:Interaction.Triggers>
<RadioButton Name="PART_ButtonFirst" Tag="1" IsChecked="True" Style="{StaticResource PaginationButtonStyle}"/>
<TextBlock Name="PART_TextBlockLeft" Margin="10,0" FontSize="16" Text="..." Foreground="{DynamicResource PrimaryTextBrush}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<StackPanel Name="PART_PanelMain" Orientation="Horizontal"/>
<TextBlock Name="PART_TextBlockRight" Margin="10,0" FontSize="16" Text="..." Foreground="{DynamicResource PrimaryTextBrush}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<RadioButton Name="PART_ButtonLast" Tag="10" Style="{StaticResource PaginationButtonStyle}"/>
</StackPanel>
<!--向右切换-->
<Button x:Name="PART_ButtonRight" Grid.Column="2" Command="interactivity:ControlCommands.Next" Margin="5,0" VerticalAlignment="Center" Style="{StaticResource ButtonCustom}">
<Border BorderThickness="1" BorderBrush="{DynamicResource BorderBrush}" CornerRadius="2" Width="30" Height="30" Background="White">
<Path Margin="10" Fill="{DynamicResource PrimaryTextBrush}" Style="{StaticResource RightPathStyle}" RenderTransformOrigin="0.5,0.5"/>
</Border>
</Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
+1
View File
@@ -9,6 +9,7 @@ namespace HandyControlDemo
{
public MainWindow()
{
InitializeComponent();
Growl.SetGrowlPanel(PanelMessage);
+2 -2
View File
@@ -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")]