WPF 之列表分頁控件

?WPF 之列表分頁控件

控件名:WindowAcrylicBlur

作者: WPFDevelopersOrg ?- 黃佳 | 驚鏵

原文鏈接: ? ?https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用大于等于.NET40

  • Visual Studio 2022

  • 項目使用 MIT 開源許可協議。

  • 新建Pagination自定義控件繼承自Control

  • 正常模式分頁 在外部套Grid分為0 - 5列:

    • Grid.Column 0 總頁數共多少300條。

    • Grid.Column 1 輸入每頁顯示多少10條。

    • Grid.Column 2 上一頁按鈕。

    • Grid.Column 3 所有頁碼按鈕此處使用ListBox

    • Grid.Column 4 下一頁按鈕。

    • Grid.Column 5 跳轉頁1碼輸入框。

  • 精簡模式分頁 在外部套Grid分為0 - 9列:

    • Grid.Column 0 總頁數共多少300條。

    • Grid.Column 2 輸入每頁顯示多少10條。

    • Grid.Column 3 條 / 頁

    • Grid.Column 5 上一頁按鈕。

    • Grid.Column 7 跳轉頁1碼輸入框。

    • Grid.Column 9 下一頁按鈕。

  • 每頁顯示跳轉頁碼數控制只允許輸入數字,不允許粘貼。

<ColumnDefinition?Width="Auto"/>
<ColumnDefinition?Width="10"/>
<ColumnDefinition?Width="Auto"/>
<ColumnDefinition?Width="Auto"/>
<ColumnDefinition?Width="10"/>
<ColumnDefinition?Width="Auto"/>
<ColumnDefinition?Width="5"/>
<ColumnDefinition?Width="Auto"/>
<ColumnDefinition?Width="5"/>
<ColumnDefinition?Width="Auto"/>
3b34fc95b3c66557bdc40002d18fa92e.png

1) Pagination.cs 如下:

using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Input;
using?WPFDevelopers.Helpers;namespace?WPFDevelopers.Controls
{[TemplatePart(Name?=?CountPerPageTextBoxTemplateName,?Type?=?typeof(TextBox))][TemplatePart(Name?=?JustPageTextBoxTemplateName,?Type?=?typeof(TextBox))][TemplatePart(Name?=?ListBoxTemplateName,?Type?=?typeof(ListBox))]public?class?Pagination?:?Control{private?const?string?CountPerPageTextBoxTemplateName?=?"PART_CountPerPageTextBox";private?const?string?JustPageTextBoxTemplateName?=?"PART_JumpPageTextBox";private?const?string?ListBoxTemplateName?=?"PART_ListBox";private?const?string?Ellipsis?=?"···";private?static?readonly?Type?_typeofSelf?=?typeof(Pagination);private?TextBox?_countPerPageTextBox;private?TextBox?_jumpPageTextBox;private?ListBox?_listBox;static?Pagination(){InitializeCommands();DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf,?new?FrameworkPropertyMetadata(_typeofSelf));}#region?Overridepublic?override?void?OnApplyTemplate(){base.OnApplyTemplate();UnsubscribeEvents();_countPerPageTextBox?=?GetTemplateChild(CountPerPageTextBoxTemplateName)?as?TextBox;if?(_countPerPageTextBox?!=?null){_countPerPageTextBox.ContextMenu?=?null;_countPerPageTextBox.PreviewTextInput?+=?_countPerPageTextBox_PreviewTextInput;_countPerPageTextBox.PreviewKeyDown?+=?_countPerPageTextBox_PreviewKeyDown;}_jumpPageTextBox?=?GetTemplateChild(JustPageTextBoxTemplateName)?as?TextBox;if?(_jumpPageTextBox?!=?null){_jumpPageTextBox.ContextMenu?=?null;_jumpPageTextBox.PreviewTextInput?+=?_countPerPageTextBox_PreviewTextInput;_jumpPageTextBox.PreviewKeyDown?+=?_countPerPageTextBox_PreviewKeyDown;}_listBox?=?GetTemplateChild(ListBoxTemplateName)?as?ListBox;Init();SubscribeEvents();}private?void?_countPerPageTextBox_PreviewKeyDown(object?sender,?KeyEventArgs?e){if?(Key.Space?==?e.Key||Key.V?==?e.Key&&?e.KeyboardDevice.Modifiers?==?ModifierKeys.Control)e.Handled?=?true;}private?void?_countPerPageTextBox_PreviewTextInput(object?sender,?TextCompositionEventArgs?e){e.Handled?=?ControlsHelper.IsNumber(e.Text);}#endregion#region?Commandprivate?static?void?InitializeCommands(){PrevCommand?=?new?RoutedCommand("Prev",?_typeofSelf);NextCommand?=?new?RoutedCommand("Next",?_typeofSelf);CommandManager.RegisterClassCommandBinding(_typeofSelf,new?CommandBinding(PrevCommand,?OnPrevCommand,?OnCanPrevCommand));CommandManager.RegisterClassCommandBinding(_typeofSelf,new?CommandBinding(NextCommand,?OnNextCommand,?OnCanNextCommand));}public?static?RoutedCommand?PrevCommand?{?get;?private?set;?}public?static?RoutedCommand?NextCommand?{?get;?private?set;?}private?static?void?OnPrevCommand(object?sender,?RoutedEventArgs?e){var?ctrl?=?sender?as?Pagination;ctrl.Current--;}private?static?void?OnCanPrevCommand(object?sender,?CanExecuteRoutedEventArgs?e){var?ctrl?=?sender?as?Pagination;e.CanExecute?=?ctrl.Current?>?1;}private?static?void?OnNextCommand(object?sender,?RoutedEventArgs?e){var?ctrl?=?sender?as?Pagination;ctrl.Current++;}private?static?void?OnCanNextCommand(object?sender,?CanExecuteRoutedEventArgs?e){var?ctrl?=?sender?as?Pagination;e.CanExecute?=?ctrl.Current?<?ctrl.PageCount;}#endregion#region?Propertiesprivate?static?readonly?DependencyPropertyKey?PagesPropertyKey?=DependencyProperty.RegisterReadOnly("Pages",?typeof(IEnumerable<string>),?_typeofSelf,new?PropertyMetadata(null));public?static?readonly?DependencyProperty?PagesProperty?=?PagesPropertyKey.DependencyProperty;public?IEnumerable<string>?Pages?=>?(IEnumerable<string>)?GetValue(PagesProperty);private?static?readonly?DependencyPropertyKey?PageCountPropertyKey?=DependencyProperty.RegisterReadOnly("PageCount",?typeof(int),?_typeofSelf,new?PropertyMetadata(1,?OnPageCountPropertyChanged));public?static?readonly?DependencyProperty?PageCountProperty?=?PageCountPropertyKey.DependencyProperty;public?int?PageCount?=>?(int)?GetValue(PageCountProperty);private?static?void?OnPageCountPropertyChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){var?ctrl?=?d?as?Pagination;var?pageCount?=?(int)?e.NewValue;/*if?(ctrl._jumpPageTextBox?!=?null)ctrl._jumpPageTextBox.Maximum?=?pageCount;*/}public?static?readonly?DependencyProperty?IsLiteProperty?=DependencyProperty.Register("IsLite",?typeof(bool),?_typeofSelf,?new?PropertyMetadata(false));public?bool?IsLite{get?=>?(bool)?GetValue(IsLiteProperty);set?=>?SetValue(IsLiteProperty,?value);}public?static?readonly?DependencyProperty?CountProperty?=?DependencyProperty.Register("Count",?typeof(int),_typeofSelf,?new?PropertyMetadata(0,?OnCountPropertyChanged,?CoerceCount));public?int?Count{get?=>?(int)?GetValue(CountProperty);set?=>?SetValue(CountProperty,?value);}private?static?object?CoerceCount(DependencyObject?d,?object?value){var?count?=?(int)?value;return?Math.Max(count,?0);}private?static?void?OnCountPropertyChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){var?ctrl?=?d?as?Pagination;var?count?=?(int)?e.NewValue;ctrl.SetValue(PageCountPropertyKey,?(int)?Math.Ceiling(count?*?1.0?/?ctrl.CountPerPage));ctrl.UpdatePages();}public?static?readonly?DependencyProperty?CountPerPageProperty?=?DependencyProperty.Register("CountPerPage",typeof(int),?_typeofSelf,?new?PropertyMetadata(50,?OnCountPerPagePropertyChanged,?CoerceCountPerPage));public?int?CountPerPage{get?=>?(int)?GetValue(CountPerPageProperty);set?=>?SetValue(CountPerPageProperty,?value);}private?static?object?CoerceCountPerPage(DependencyObject?d,?object?value){var?countPerPage?=?(int)?value;return?Math.Max(countPerPage,?1);}private?static?void?OnCountPerPagePropertyChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){var?ctrl?=?d?as?Pagination;var?countPerPage?=?(int)?e.NewValue;if?(ctrl._countPerPageTextBox?!=?null)ctrl._countPerPageTextBox.Text?=?countPerPage.ToString();ctrl.SetValue(PageCountPropertyKey,?(int)?Math.Ceiling(ctrl.Count?*?1.0?/?countPerPage));if?(ctrl.Current?!=?1)ctrl.Current?=?1;elsectrl.UpdatePages();}public?static?readonly?DependencyProperty?CurrentProperty?=?DependencyProperty.Register("Current",?typeof(int),_typeofSelf,?new?PropertyMetadata(1,?OnCurrentPropertyChanged,?CoerceCurrent));public?int?Current{get?=>?(int)?GetValue(CurrentProperty);set?=>?SetValue(CurrentProperty,?value);}private?static?object?CoerceCurrent(DependencyObject?d,?object?value){var?current?=?(int)?value;var?ctrl?=?d?as?Pagination;return?Math.Max(current,?1);}private?static?void?OnCurrentPropertyChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){var?ctrl?=?d?as?Pagination;var?current?=?(int)?e.NewValue;if?(ctrl._listBox?!=?null)ctrl._listBox.SelectedItem?=?current.ToString();if?(ctrl._jumpPageTextBox?!=?null)ctrl._jumpPageTextBox.Text?=?current.ToString();ctrl.UpdatePages();}#endregion#region?Event///?<summary>///?????分頁///?</summary>private?void?OnCountPerPageTextBoxChanged(object?sender,?TextChangedEventArgs?e){if?(int.TryParse(_countPerPageTextBox.Text,?out?var?_ountPerPage))CountPerPage?=?_ountPerPage;}///?<summary>///?????跳轉頁///?</summary>private?void?OnJumpPageTextBoxChanged(object?sender,?TextChangedEventArgs?e){if?(int.TryParse(_jumpPageTextBox.Text,?out?var?_current))Current?=?_current;}///?<summary>///?????選擇頁///?</summary>private?void?OnSelectionChanged(object?sender,?SelectionChangedEventArgs?e){if?(_listBox.SelectedItem?==?null)return;Current?=?int.Parse(_listBox.SelectedItem.ToString());}#endregion#region?Privateprivate?void?Init(){SetValue(PageCountPropertyKey,?(int)?Math.Ceiling(Count?*?1.0?/?CountPerPage));_jumpPageTextBox.Text?=?Current.ToString();//_jumpPageTextBox.Maximum?=?PageCount;_countPerPageTextBox.Text?=?CountPerPage.ToString();if?(_listBox?!=?null)_listBox.SelectedItem?=?Current.ToString();}private?void?UnsubscribeEvents(){if?(_countPerPageTextBox?!=?null)_countPerPageTextBox.TextChanged?-=?OnCountPerPageTextBoxChanged;if?(_jumpPageTextBox?!=?null)_jumpPageTextBox.TextChanged?-=?OnJumpPageTextBoxChanged;if?(_listBox?!=?null)_listBox.SelectionChanged?-=?OnSelectionChanged;}private?void?SubscribeEvents(){if?(_countPerPageTextBox?!=?null)_countPerPageTextBox.TextChanged?+=?OnCountPerPageTextBoxChanged;if?(_jumpPageTextBox?!=?null)_jumpPageTextBox.TextChanged?+=?OnJumpPageTextBoxChanged;if?(_listBox?!=?null)_listBox.SelectionChanged?+=?OnSelectionChanged;}private?void?UpdatePages(){SetValue(PagesPropertyKey,?GetPagers(Count,?Current));if?(_listBox?!=?null?&&?_listBox.SelectedItem?==?null)_listBox.SelectedItem?=?Current.ToString();}private?IEnumerable<string>?GetPagers(int?count,?int?current){if?(count?==?0)return?null;if?(PageCount?<=?7)return?Enumerable.Range(1,?PageCount).Select(p?=>?p.ToString()).ToArray();if?(current?<=?4)return?new[]?{"1",?"2",?"3",?"4",?"5",?Ellipsis,?PageCount.ToString()};if?(current?>=?PageCount?-?3)return?new[]{"1",?Ellipsis,?(PageCount?-?4).ToString(),?(PageCount?-?3).ToString(),?(PageCount?-?2).ToString(),(PageCount?-?1).ToString(),?PageCount.ToString()};return?new[]{"1",?Ellipsis,?(current?-?1).ToString(),?current.ToString(),?(current?+?1).ToString(),?Ellipsis,PageCount.ToString()};}#endregion}
}

2) Pagination.xaml 如下:

<ResourceDictionary?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"xmlns:helpers="clr-namespace:WPFDevelopers.Helpers"xmlns:controls="clr-namespace:WPFDevelopers.Controls"><ResourceDictionary.MergedDictionaries><ResourceDictionary?Source="Basic/ControlBasic.xaml"/></ResourceDictionary.MergedDictionaries><Style?x:Key="PageListBoxStyleKey"?TargetType="{x:Type?ListBox}"?BasedOn="{StaticResource?ControlBasicStyle}"><Setter?Property="Background"?Value="Transparent"/><Setter?Property="BorderThickness"?Value="0"/><Setter?Property="Padding"?Value="0"/><Setter?Property="Template"><Setter.Value><ControlTemplate?TargetType="{x:Type?ListBox}"><Border?BorderBrush="{TemplateBinding?BorderBrush}"?BorderThickness="{TemplateBinding?BorderThickness}"?Background="{TemplateBinding?Background}"?SnapsToDevicePixels="True"><ScrollViewer?Focusable="False"?Padding="{TemplateBinding?Padding}"><ItemsPresenter?SnapsToDevicePixels="{TemplateBinding?SnapsToDevicePixels}"/></ScrollViewer></Border><ControlTemplate.Triggers><Trigger?Property="IsGrouping"?Value="True"><Setter?Property="ScrollViewer.CanContentScroll"?Value="False"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><Style?x:Key="PageListBoxItemStyleKey"?TargetType="{x:Type?ListBoxItem}"BasedOn="{StaticResource?ControlBasicStyle}"><Setter?Property="MinWidth"?Value="32"/><Setter?Property="Cursor"?Value="Hand"/><Setter?Property="HorizontalContentAlignment"?Value="Center"/><Setter?Property="VerticalContentAlignment"?Value="Center"/><Setter?Property="helpers:ElementHelper.CornerRadius"?Value="3"/><Setter?Property="BorderThickness"?Value="1"/><Setter?Property="Padding"?Value="5,0"/><Setter?Property="Margin"?Value="3,0"/><Setter?Property="Background"?Value="{DynamicResource?BackgroundSolidColorBrush}"/><Setter?Property="BorderBrush"?Value="{DynamicResource?BaseSolidColorBrush}"/><Setter?Property="Template"><Setter.Value><ControlTemplate?TargetType="{x:Type?ListBoxItem}"><Border?SnapsToDevicePixels="True"Background="{TemplateBinding?Background}"?BorderThickness="{TemplateBinding?BorderThickness}"?BorderBrush="{TemplateBinding?BorderBrush}"??Padding="{TemplateBinding?Padding}"CornerRadius="{Binding?Path=(helpers:ElementHelper.CornerRadius),RelativeSource={RelativeSource?TemplatedParent}}"><ContentPresenter?x:Name="PART_ContentPresenter"?HorizontalAlignment="{TemplateBinding?HorizontalContentAlignment}"?VerticalAlignment="{TemplateBinding?VerticalContentAlignment}"RecognizesAccessKey="True"?TextElement.Foreground="{TemplateBinding?Foreground}"/></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><DataTrigger?Binding="{Binding?.}"?Value="···"><Setter?Property="IsEnabled"?Value="False"/><Setter?Property="FontWeight"?Value="Bold"/></DataTrigger><Trigger?Property="IsMouseOver"?Value="True"><Setter?Property="BorderBrush"?Value="{DynamicResource?DefaultBorderBrushSolidColorBrush}"/><Setter?Property="Background"?Value="{DynamicResource?DefaultBackgroundSolidColorBrush}"/><Setter?Property="Foreground"?Value="{DynamicResource?PrimaryNormalSolidColorBrush}"/></Trigger><Trigger?Property="IsSelected"?Value="True"><Setter?Property="Background"?Value="{DynamicResource?PrimaryPressedSolidColorBrush}"/><Setter?Property="TextElement.Foreground"?Value="{DynamicResource?WindowForegroundColorBrush}"/></Trigger></Style.Triggers></Style><ControlTemplate?x:Key="LitePagerControlTemplate"?TargetType="{x:Type?controls:Pagination}"><Border?Background="{TemplateBinding?Background}"BorderBrush="{TemplateBinding?BorderBrush}"BorderThickness="{TemplateBinding?BorderThickness}"Padding="{TemplateBinding?Padding}"><Grid><Grid.ColumnDefinitions><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="10"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="10"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="5"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="5"/><ColumnDefinition?Width="Auto"/></Grid.ColumnDefinitions><TextBlock?VerticalAlignment="Center"Text="{Binding?Count,StringFormat=共?{0}?條,RelativeSource={RelativeSource?TemplatedParent}}"/><TextBox?Grid.Column="2"?x:Name="PART_CountPerPageTextBox"?TextAlignment="Center"?VerticalContentAlignment="Center"Width="60"?MinWidth="0"input:InputMethod.IsInputMethodEnabled="False"/><TextBlock?Grid.Column="3"?Text="?條?/?頁"?VerticalAlignment="Center"/><Button?Grid.Column="5"?Command="{x:Static?controls:Pagination.PrevCommand}"><Path?Width="7"?Height="10"?Stretch="Fill"?Fill="{Binding?Foreground,RelativeSource={RelativeSource?AncestorType=Button}}"Data="{StaticResource?PathPrevious}"/></Button><TextBox?Grid.Column="7"?x:Name="PART_JumpPageTextBox"?TextAlignment="Center"?VerticalContentAlignment="Center"Width="60"?MinWidth="0"><TextBox.ToolTip><TextBlock><TextBlock.Text><MultiBinding?StringFormat="{}{0}/{1}"><Binding?Path="Current"?RelativeSource="{RelativeSource?TemplatedParent}"/><Binding?Path="PageCount"?RelativeSource="{RelativeSource?TemplatedParent}"/></MultiBinding></TextBlock.Text></TextBlock></TextBox.ToolTip></TextBox><Button?Grid.Column="9"?Command="{x:Static?controls:Pagination.NextCommand}"><Path?Width="7"?Height="10"?Stretch="Fill"?Fill="{Binding?Foreground,RelativeSource={RelativeSource?AncestorType=Button}}"Data="{StaticResource?PathNext}"/></Button></Grid></Border></ControlTemplate><Style?TargetType="{x:Type?controls:Pagination}"?BasedOn="{StaticResource?ControlBasicStyle}"><Setter?Property="Template"><Setter.Value><ControlTemplate?TargetType="{x:Type?controls:Pagination}"><Border?Background="{TemplateBinding?Background}"BorderBrush="{TemplateBinding?BorderBrush}"BorderThickness="{TemplateBinding?BorderThickness}"Padding="{TemplateBinding?Padding}"><Grid><Grid.ColumnDefinitions><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="*"/><ColumnDefinition?Width="Auto"/><ColumnDefinition?Width="Auto"/></Grid.ColumnDefinitions><TextBlock?Margin="0,0,15,0"?VerticalAlignment="Center"Text="{Binding?Count,StringFormat=共?{0}?條,RelativeSource={RelativeSource?TemplatedParent}}"/><StackPanel?Grid.Column="1"?Orientation="Horizontal"?Margin="0,0,15,0"?><TextBlock?Text="每頁?"?VerticalAlignment="Center"/><TextBox?x:Name="PART_CountPerPageTextBox"?TextAlignment="Center"?Width="60"MinWidth="0"?VerticalContentAlignment="Center"FontSize="{TemplateBinding?FontSize}"?input:InputMethod.IsInputMethodEnabled="False"/><TextBlock?Text="?條"?VerticalAlignment="Center"/></StackPanel><Button?Grid.Column="2"?Command="{x:Static?controls:Pagination.PrevCommand}"><Path?Width="7"?Height="10"?Stretch="Fill"?Fill="{Binding?Foreground,RelativeSource={RelativeSource?AncestorType=Button}}"Data="{StaticResource?PathPrevious}"/></Button><ListBox?x:Name="PART_ListBox"?Grid.Column="3"SelectedIndex="0"?Margin="5,0"ItemsSource="{TemplateBinding?Pages}"Style="{StaticResource?PageListBoxStyleKey}"ItemContainerStyle="{StaticResource?PageListBoxItemStyleKey}"ScrollViewer.HorizontalScrollBarVisibility="Hidden"ScrollViewer.VerticalScrollBarVisibility="Hidden"><ListBox.ItemsPanel><ItemsPanelTemplate><UniformGrid?Rows="1"/></ItemsPanelTemplate></ListBox.ItemsPanel></ListBox><Button?Grid.Column="4"?Command="{x:Static?controls:Pagination.NextCommand}"><Path?Width="7"?Height="10"?Stretch="Fill"?Fill="{Binding?Foreground,RelativeSource={RelativeSource?AncestorType=Button}}"Data="{StaticResource?PathNext}"/></Button><StackPanel?Grid.Column="5"?Orientation="Horizontal"><TextBlock?Text="?前往?"?VerticalAlignment="Center"/><TextBox?x:Name="PART_JumpPageTextBox"TextAlignment="Center"?ContextMenu="{x:Null}"Width="60"?VerticalContentAlignment="Center"MinWidth="0"FontSize="{TemplateBinding?FontSize}"?/><TextBlock?Text="?頁"?VerticalAlignment="Center"/></StackPanel></Grid></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><Trigger?Property="IsLite"?Value="true"><Setter?Property="Template"?Value="{StaticResource?LitePagerControlTemplate}"/></Trigger></Style.Triggers></Style></ResourceDictionary>

3) 創建PaginationExampleVM.cs如下:

using?System.Collections.Generic;
using?System.Collections.ObjectModel;
using?System.Linq;namespace?WPFDevelopers.Samples.ViewModels
{public?class?PaginationExampleVM?:?ViewModelBase{private?List<int>?_sourceList?=?new?List<int>();public?PaginationExampleVM(){_sourceList.AddRange(Enumerable.Range(1,?300));Count?=?300;CurrentPageChanged();}public?ObservableCollection<int>?PaginationCollection?{?get;?set;?}?=?new?ObservableCollection<int>();private?int?_count;public?int?Count{get?{?return?_count;?}set?{?_count?=?value;??this.NotifyPropertyChange("Count");?CurrentPageChanged();?}}private?int?_countPerPage?=?10;public?int?CountPerPage{get?{?return?_countPerPage;?}set?{?_countPerPage?=?value;?this.NotifyPropertyChange("CountPerPage");?CurrentPageChanged();?}}private?int?_current?=?1;public?int?Current{get?{?return?_current;?}set?{?_current?=?value;?this.NotifyPropertyChange("Current");?CurrentPageChanged();?}}private?void?CurrentPageChanged(){PaginationCollection.Clear();foreach?(var?i?in?_sourceList.Skip((Current?-?1)?*?CountPerPage).Take(CountPerPage)){PaginationCollection.Add(i);}}}
}

4) 使用 PaginationExample.xaml 如下:

<UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.PaginationExample"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"?xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"mc:Ignorable="d"?d:DesignHeight="450"?d:DesignWidth="800"><UserControl.Resources><Style?TargetType="{x:Type?TextBlock}"><Setter?Property="Foreground"?Value="{DynamicResource?PrimaryTextSolidColorBrush}"?/><Setter?Property="FontSize"?Value="{StaticResource?NormalFontSize}"/><Setter?Property="VerticalAlignment"?Value="Center"/></Style></UserControl.Resources><Grid><Grid.RowDefinitions><RowDefinition?Height="50"/><RowDefinition/><RowDefinition?Height="40"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition?Width="2*"/><ColumnDefinition?Width="30"/><ColumnDefinition?Width="*"/></Grid.ColumnDefinitions><TextBlock?Text="正常模式分頁"?HorizontalAlignment="Center"?VerticalAlignment="Center"/><TextBlock?Grid.Column="2"?Text="精簡模式分頁"?HorizontalAlignment="Center"?VerticalAlignment="Center"/><ListBox?Grid.Row="1"?Grid.Column="0"?ItemsSource="{Binding?NormalPaginationViewModel.PaginationCollection}"?Margin="20,0,0,0"/><ListBox?Grid.Row="1"?Grid.Column="2"?ItemsSource="{Binding?LitePaginationViewModel.PaginationCollection}"?Margin="0,0,20,0"/><wpfdev:Pagination?Grid.Row="2"?Grid.Column="0"?IsLite="False"??Margin="20,0,0,0"Count="{Binding?NormalPaginationViewModel.Count,Mode=TwoWay}"CountPerPage="{Binding?NormalPaginationViewModel.CountPerPage,Mode=TwoWay}"Current="{Binding?NormalPaginationViewModel.Current,Mode=TwoWay}"/><wpfdev:Pagination?Grid.Row="2"?Grid.Column="2"?IsLite="true"??Margin="0,0,20,0"Count="{Binding?LitePaginationViewModel.Count,Mode=TwoWay}"CountPerPage="{Binding?LitePaginationViewModel.CountPerPage,Mode=TwoWay}"Current="{Binding?LitePaginationViewModel.Current,Mode=TwoWay}"/></Grid>
</UserControl>

5) 使用PaginationExample.xaml.cs如下:

using?System.Windows.Controls;
using?WPFDevelopers.Samples.ViewModels;namespace?WPFDevelopers.Samples.ExampleViews
{///?<summary>///?PaginationExample.xaml?的交互邏輯///?</summary>public?partial?class?PaginationExample?:?UserControl{public?PaginationExampleVM?NormalPaginationViewModel?{?get;?set;?}?=?new?PaginationExampleVM();public?PaginationExampleVM?LitePaginationViewModel?{?get;?set;?}?=?new?PaginationExampleVM();public?PaginationExample(){InitializeComponent();this.DataContext?=?this;}}
}

?鳴謝 - 黃佳

4674f5eab2adf7c3d8fcc2f7fc065302.gif

Github|Pagination[1]
碼云|Pagination[2]

參考資料

[1]

Github|PaginationExample: https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/PaginationExample.xaml

[2]

碼云|PaginationExample: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/PaginationExample.xaml

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/282058.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/282058.shtml
英文地址,請注明出處:http://en.pswp.cn/news/282058.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

高并發下防止庫存超賣解決方案

一、概述 目前網上關于防止庫存超賣&#xff0c;我沒找到可以支持一次購買多件的&#xff0c;都是基于一次只能購買一件做的秒殺方案&#xff0c;但是實際場景中&#xff0c;一般秒殺活動都是支持&#xff11;&#xff5e;&#xff15;件的&#xff0c;因此為了補缺&#xff0…

【幾何/分治】【最短路】【數學期望】Day 10.24

1、斜率 可以證明如果兩點之間還有一點的話那么原來的兩個點連線一定不會是最大斜率 然后我就寫了個沙茶分治………… 其實根據上面的推論只用枚舉相鄰的兩個點&#xff0c;掃一遍就可以了 1 #include <cstdio>2 #include <algorithm>3 #include <iostream>4…

K8s 介紹

過去一段時間&#xff0c;公司事情比較多&#xff0c;現在稍微能好點&#xff0c;今天進一步驗證自己K8S 集群環境&#xff0c;遇到不少問題&#xff0c; 發現從自己的master 上無法訪問node 的pod&#xff0c; 然后一堆search 。 config 。。 [rootk8s-master ~]# systemctl s…

easypoi needmerge失效_EasyPOI簡單用例,簡單有效

用poi導出Excel表格&#xff0c;需要配置很多東西&#xff0c;也比較麻煩&#xff0c;這里使用poi的封裝easypoi&#xff0c;可以快速配置&#xff0c;實現Excel或者word文件的導出。這里我們結合SpringMVC開發easypoi。1&#xff0c;導入以下3個.jar包:這里是springMVC和easyp…

禁止sethc.exe運行 防止3389的sethc后門

廢話&#xff1a;在土司看到的一篇文章,發私信給那個哥們兒說讓不讓轉載,結果還沒回復我就在百度看到相同的文章。他自己也是轉載的。這哥們兒ID遲早被ban 文章轉載自:http://www.jb51.net/hack/64484.html 點“開始”&#xff0c;在“運行”中敲入gpedit.msc依次展開“用戶配置…

Mac 與虛擬機中的linux集群共享文件目錄設置

一、環境介紹 本機&#xff1a;Macos Big Sur系統 虛擬機軟件&#xff1a;vmware-fusion 虛擬機上虛擬的linux - centos7 系統 二、實現的效果 在mac上創建一個/Users/SH-Server/vm-vagrant目錄&#xff0c;作為之后和虛擬機linux系統 /data 文件夾的共享目錄。 我們最終想…

jsp編程技術徐天鳳課后答案_jsp編程技術教材課后習題.doc

jsp編程技術教材課后習題JSP編程技術習題集1.6 本 章 習 題思考題(1)為什么要為JDK設置環境變量&#xff1f;(2)Tomcat和JDK是什么關系&#xff1f;(3)什么是Web服務根目錄、子目錄、相對目錄&#xff1f;如何配置虛擬目錄&#xff1f;(4)什么是B/S模式&#xff1f;(5)JSP、Jav…

JVM知識(一)

java三大流&#xff1a;數據流、控制流、指令流 線程是執行程序的最小單元&#xff0c;一個線程中也有這些東西。 java 運行時數據區&#xff1a; 1.程序計數器 指向當前線程正在執行的字節碼指令地址。如果此時從一個線程轉為執行另一個線程&#xff0c;此時就會中斷&#xff…

AWD-LSTM為什么這么棒?

摘要&#xff1a; AWD-LSTM為什么這么棒&#xff0c;看完你就明白啦&#xff01;AWD-LSTM是目前最優秀的語言模型之一。在眾多的頂會論文中&#xff0c;對字級模型的研究都采用了AWD-LSTMs&#xff0c;并且它在字符級模型中的表現也同樣出色。 本文回顧了論文——Regularizing …

Spread / Rest 操作符

Spread / Rest 操作符指的是 ...&#xff0c;具體是 Spread 還是 Rest 需要看上下文語境。 當被用于迭代器中時&#xff0c;它是一個 Spread 操作符&#xff1a;&#xff08;參數為數組&#xff09; function foo(x,y,z) {console.log(x,y,z); }let arr [1,2,3]; foo(...arr);…

python postman腳本自動化_如何用Postman做接口自動化測試

什么是自動化測試把人對軟件的測試行為轉化為由機器執行測試行為的一種實踐。例如GUI自動化測試&#xff0c;模擬人去操作軟件界面&#xff0c;把人從簡單重復的勞動中解放出來本質是用代碼去測試另一段代碼&#xff0c;屬于一種軟件開發工作&#xff0c;已經開發完成的用例還必…

Mac上,為虛擬機集群上的每臺虛擬機設置固定IP

一、環境介紹 本機&#xff1a;macOS系統 虛擬機軟件&#xff1a;VMware Fusion 虛擬機上&#xff1a;centos7內核的Linux系統集群 二、為什么要為每臺虛擬機設置固定ip 由于每次啟動虛擬機&#xff0c;得到的ip可能不一樣&#xff0c;這樣對遠程連接非常不友好&#xff0c…

朱曄的互聯網架構實踐心得S1E7:三十種架構設計模式(上)

設計模式是前人通過大量的實踐總結出來的一些經驗總結和最佳實踐。在經過多年的軟件開發實踐之后&#xff0c;回過頭來去看23種設計模式你會發現很多平時寫代碼的套路和OO的套路和設計模式里總結的類似&#xff0c;這也說明了你悟到的東西和別人悟到的一樣&#xff0c;經過大量…

記一次某制造業ERP系統 CPU打爆事故分析

一&#xff1a;背景 1.講故事前些天有位朋友微信找到我&#xff0c;說他的程序出現了CPU階段性爆高&#xff0c;過了一會就下去了&#xff0c;咨詢下這個爆高階段程序內部到底發生了什么&#xff1f;畫個圖大概是下面這樣&#xff0c;你懂的。按經驗來說&#xff0c;這種情況一…

PC端和移動APP端CSS樣式初始化

CSS樣式初始化分為PC端和移動APP端 1.PC端&#xff1a;使用Normalize.css Normalize.css是一種CSS reset的替代方案。 我們創造normalize.css有下面這幾個目的&#xff1a; 保護有用的瀏覽器默認樣式而不是完全去掉它們一般化的樣式&#xff1a;為大部分HTML元素提供修復瀏覽器…

FPGA浮點數定點化

因為在普通的fpga芯片里面&#xff0c;寄存器只可以表示無符號型&#xff0c;不可以表示小數&#xff0c;所以在計算比較精確的數值時&#xff0c;就需要做一些處理&#xff0c;不過在altera在Arria 10 中增加了硬核浮點DSP模塊&#xff0c;這樣更加適合硬件加速和做一些比較精…

框架實現修改功能的原理_JAVA集合框架的特點及實現原理簡介

1.集合框架總體架構集合大致分為Set、List、Queue、Map四種體系,其中List,Set,Queue繼承自Collection接口&#xff0c;Map為獨立接口Set的實現類有:HashSet&#xff0c;LinkedHashSet&#xff0c;TreeSet...List下有ArrayList&#xff0c;Vector&#xff0c;LinkedList...Map下…

NPM報錯終極大法

2019獨角獸企業重金招聘Python工程師標準>>> 所有的錯誤基本上都跟node的版本相關 直接刪除系統中的node 重新安裝 sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*} 重新安裝 $ n lts $ npm install -g npm $ n stable…

自己使用的一個.NET輕量開發結構

三個文件夾&#xff0c;第一個是放置前端部分&#xff0c;第二個是各種支持的類文件&#xff0c;第三個是單元測試文件。Core文件類庫放置的是與數據庫做交互的文件&#xff0c;以及一些第三方類庫&#xff0c;還有與數據庫連接的文件1.Lasy.Validator是一個基于Attribute驗證器…

英語影視臺詞---八、the shawshank redemption

英語影視臺詞---八、the shawshank redemption 一、總結 一句話總結&#xff1a;肖申克的救贖 1、Its funny. On the outside, I was an honest man. Straight as an arrow. I had to come to prison to be a crook.&#xff1f; 這很有趣。 在外面&#xff0c;我是一個誠實的人…