WPF之列表分页控件框架使用大于等于.NET40。VisualStudio2022。项目使用MIT开源许可协议。新建Pagination自定义控件继承自Control。正常模式分页在外部套Gri...
wpF 之列表分页控件
框架使用大于等于.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 下一页按钮。每页显示与跳转页码数控制只允许输入数字,不允许粘贴。
实现代码
<ColumnDefinitionWidth="Auto"/> <ColumnDefinitionWidth="10"/> <ColumnDefinitionWidth="Auto"/> <ColumnDefinitionWidth="Auto"/> <ColumnDefinitionWidth="10"/> <ColumnDefinitionWidth="Auto"/> <ColumnDefinitionWidth="5"/> <ColumnDefinitionWidth="Auto"/> <ColumnDefinitionWidth="5"/> <ColumnDefinitionWidth="Auto"/>

1) Pagination.cs 如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Input;
usingWPFDevelopers.Helpers;
namespaceWPFDevelopers.Controls
{
[TemplatePart(Name=CountPerPageTextBoxTemplateName,Type=typeof(TextBox))]
[TemplatePart(Name=JustPageTextBoxTemplateName,Type=typeof(TextBox))]
[TemplatePart(Name=ListBoxTemplateName,Type=typeof(ListBox))]
publicclassPagination:Control
{
privateconststringCountPerPageTextBoxTemplateName="PART_CountPerPageTextBox";
privateconststringJustPageTextBoxTemplateName="PART_JumpPageTextBox";
privateconststringListBoxTemplateName="PART_ListBox";
privateconststringEllipsis="";
privatestaticreadonlyType_typeofSelf=typeof(Pagination);
privateTextBox_countPerPageTextBox;
privateTextBox_jumpPageTextBox;
privateListBox_listBox;
staticPagination()
{
InitializeCommands();
DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf,newFrameworkPropertyMetadata(_typeofSelf));
}
#regionOverride
publicoverridevoidOnApplyTemplate()
{
base.OnApplyTemplate();
UnsubscribeEvents();
_countPerPageTextBox=GetTemplateChild(CountPerPageTextBoxTemplateName)asTextBox;
if(_countPerPageTextBox!=null)
{
_countPerPageTextBox.ContextMenu=null;
_countPerPageTextBox.PreviewTextInput+=_countPerPageTextBox_PreviewTextInput;
_countPerPageTextBox.PreviewKeyDown+=_countPerPageTextBox_PreviewKeyDown;
}
_jumpPageTextBox=GetTemplateChild(JustPageTextBoxTemplateName)asTextBox;
if(_jumpPageTextBox!=null)
{
_jumpPageTextBox.ContextMenu=null;
_jumpPageTextBox.PreviewTextInput+=_countPerPageTextBox_PreviewTextInput;
_jumpPageTextBox.PreviewKeyDown+=_countPerPageTextBox_PreviewKeyDown;
}
_listBox=GetTemplateChild(ListBoxTemplateName)asListBox;
Init();
SubscribeEvents();
}
privatevoid_countPerPageTextBox_PreviewKeyDown(objectsender,KeyEventArgse)
{
if(Key.Space==e.Key
||
Key.V==e.Key
&&e.KeyboardDevice.Modifiers==ModifierKeys.Control)
e.Handled=true;
}
privatevoid_countPerPageTextBox_PreviewTextInput(objectsender,TextCompositionEventArgse)
{
e.Handled=ControlsHelper.IsNumber(e.Text);
}
#endregion
#regionCommand
privatestaticvoidInitializeCommands()
{
PrevCommand=newRoutedCommand("Prev",_typeofSelf);
NextCommand=newRoutedCommand("Next",_typeofSelf);
CommandManager.RegisterClassCommandBinding(_typeofSelf,
newCommandBinding(PrevCommand,OnPrevCommand,OnCanPrevCommand));
CommandManager.RegisterClassCommandBinding(_typeofSelf,
newCommandBinding(NextCommand,OnNextCommand,OnCanNextCommand));
}
publicstaticRoutedCommandPrevCommand{get;privateset;}
publicstaticRoutedCommandNextCommand{get;privateset;}
privatestaticvoidOnPrevCommand(objectsender,RoutedEventArgse)
{
varctrl=senderASPagination;
ctrl.Current--;
}
privatestaticvoidOnCanPrevCommand(objectsender,CanExecuteRoutedEventArgse)
{
varctrl=senderasPagination;
e.CanExecute=ctrl.Current>1;
}
privatestaticvoidOnNextCommand(objectsender,RoutedEventArgse)
{
varctrl=senderasPagination;
ctrl.Current++;
}
privatestaticvoidOnCanNextCommand(objectsender,CanExecuteRoutedEventArgse)
{
varctrl=senderasPagination;
e.CanExecute=ctrl.Current<ctrl.PageCount;
}
#endregion
#regionProperties
privatestaticreadonlyDependencyPropertyKeyPagesPropertyKey=
DependencyProperty.RegisterReadOnly("Pages",typeof(IEnumerable<string>),_typeofSelf,
newPropertyMetadata(null));
publicstaticreadonlyDependencyPropertyPagesProperty=PagesPropertyKey.DependencyProperty;
publicIEnumerable<string>Pages=>(IEnumerable<string&g编程t;)GetValue(PagesProperty);
privatestaticreadonlyDependencyPropertyKeyPageCountPropertyKey=
DependencyProperty.RegisterReadOnly("PageCount",typeof(int),_typeofSelf,
newPropertyMetadata(1,OnPageCountPropertyChanged));
publicstaticreadonlyDependencyPropertyPageCountProperty=PageCountPropertyKey.DependencyProperty;
publicintPageCount=>(int)GetValue(PageCountProperty);
privatestaticvoidOnPageCountPropertyChanged(DependencyObjectd,DependencyPropertyChangedEventArgse)
{
varctrl=dasPagination;
varpageCount=(int)e.NewValue;
/*
if(ctrl._jumpPageTextBox!=null)
ctrl._jumpPageTextBox.Maximum=pageCount;
*/
}
publicstaticreadonlyDependencyPropertyIsLiteProperty=
DependencyProperty.Register("IsLite",typeof(bool),_typeofSelf,newPropertyMetadata(false));
publicboolIsLite
{
get=>(bool)GetValue(IsLiteProperty);
set=>SetValue(IsLiteProperty,value);
}
publicstaticreadonlyDependencyPropertyCountProperty=DependencyProperty.Register("Count",typeof(int),
_typeofSelf,newPropertyMetadata(0,OnCountPropertyChanged,CoerceCount));
publicintCount
{
get=>(int)GetValue(CountProperty);
set=>SetValue(CountProperty,value);
}
privatestaticobjectCoerceCount(DependencyObjectd,objectvalue)
{
varcount=(int)value;
returnMath.Max(count,0);
}
privatestaticvoidOnCountPropertyChanged(DependencyObjectd,DependencyPropertyChangedEventArgse)
{
varctrl=dasPagination;
varcount=(int)e.NewValue;
ctrl.SetValue(PageCountPropertyKey,(int)Math.Ceiling(count*1.0/ctrl.CountPerPage));
ctrl.UpdatePages();
}
publicstaticreadonlyDependencyPropertyCountPerPageProperty=DependencyProperty.Register("CountPerPage",
typeof(int),_typeofSelf,newPropertyMetadata(50,OnCountPerPagePropertyChanged,CoerceCountPerPage));
publicintCountPerPage
{
get=>(int)GetValue(CountPerPageProperty);
set=>SetValue(CountPerPageProperty,value);
}
privatestaticobjectCoerceCountPerPage(DependencyObjectd,objectvalue)
{
varcountPerPage=(int)value;
returnMath.Max(countPerPage,1);
}
privatestaticvoidOnCountPerPagePropertyChanged(DependencyObjectd,DependencyPropertyChangedEventArgse)
{
varctrl=dasPagination;
varcountPerPage=(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;
else
ctrl.UpdatePages();
}
publicstaticreadonlyDependencyPropertyCurrentProperty=DependencyProperty.Register("Current",typeof(int),
_typeofSelf,newPropertyMetadata(1,OnCurrentPropertyChanged,CoerceCurrent));
publicintCurrent
{
get=>(int)GetValue(CurrentProperty);
set=>SetValue(CurrentProperty,value);
}
privatestaticobjectCoerceCurrent(DependencyObjectd,objectvalue)
{
varcurrent=(int)value;
varctrl=dasPagination;
returnMath.Max(current,1);
}
privatestaticvoidOnCurrentPropertyChanged(DependencyObjectd,DependencyPropertyChangedEventArgse)
{
varctrl=dasPagination;
varcurrent=(int)e.NewValue;
if(ctrl._listBox!=null)
ctrl._listBox.SelectedItem=current.ToString();
if(ctrl._jumpPageTextBox!=null)
ctrl._jumpPageTextBox.Text=current.ToString();
ctrl.UpdatePages();
}
#endregion
#regionEvent
///<summary>
///分页
///</summary>
privatevoidOnCountPerPageTextBoxChanged(objectsender,TextChangedEventArgse)
{
if(int.TryParse(_countPerPageTextBox.Text,outvar_ountPerPage))
CountPerPage=_ountPerPage;
}
///<summary>
///跳转页
///</summary>
privatevoidOnJumpPageTextBoxChanged(objectsender,TextChangedEventArgse)
{
if(int.TryParse(_jumpPageTextBox.Text,outvar_current))
Current=_current;
}
///<summary>
///选择页
///</summary>
privatevoidOnSelectionChanged(objectsender,SelectionChangedEventArgse)
{
if(_listBox.SelectedItem==null)
return;
Current=int.Parse(_listBox.SelectedItem.ToString());
}
#endregion
#regionPrivate
privatevoidInit()
{
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();
}
privatevoidUnsubscribeEvents()
{
if(_countPerPageTextBox!=null)
_countPerPageTextBox.TextChanged-=OnCountPerPageTextBoxChanged;
if(_jumpPageTextBox!=null)
_jumpPageTextBox.TextChanged-=OnJumpPageTextBoxChanged;
if(_listBox!=null)
_listBox.SelectionChanged-=OnSelectionChanged;
}
privatevoidSubscribeEvents()
{
if(_countPerPageTextBox!=null)
_countPerPageTextBox.TextChanged+=OnCountPerPageTextBoxChanged;
if(_jumpPageTextBox!=null)
_jumpPageTextBox.TextChanged+=OnJumpPageTextBoxChanged;
if(_listBox!=null)
_listBox.SelectionChanged+=OnSelectionChanged;
}
privatevoidUpdatePages()
{
SetValue(PagesPropertyKey,GetPagers(Count,CurrenwfjMCGEXt));
if(_listBox!=null&&_listBox.SelectedItem==null)
_listBox.SelectedItem=Current.ToString();
}
privateIEnumerable<string>GetPagers(intcount,intcurrent)
{
if(count==0)
returnnull;
if(PageCount<=7)
returnEnumerable.Range(1,PageCount).Select(p=>p.ToString()).ToArray();
if(current<=4)
returnnew[]{"1","2","3","4","5",Ellipsis,PageCount.ToString()};
if(current>=PageCount-3)
returnnew[]
{
"1",Ellipsis,(PageCount-4).ToString(),(PageCount-3).ToString(),(PageCount-2).ToString(),
(PageCount-1).ToString(),PageCount.ToString()
};
returnnew[]
{
"1",Ellipsis,(current-1).ToString(),current.ToString(),(current+1).ToString(),Ellipsis,
PageCount.ToString()
};
}
#endregion
}
}
2) Pagination.xaml 如下:
<ResourceDictionaryXMLns="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>
<ResourceDictionarySource="Basic/ControlBasic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Stylex:Key="PageListBoxStyleKey"TargetType="{x:TypeListBox}"
BasedOn="{StaticResourceControlBasicStyle}">
<SetterProperty="Background"Value="Transparent"/>
<SetterProperty="BorderThickness"Value="0"/>
<SetterProperty="Padding"Value="0"/>
<SetterProperty="Template">
<Setter.Value>
<ControlTemplateTargetType="{x:TypeListBox}">
<BorderBorderBrush="{TemplateBindingBorderBrush}"
BorderThickness="{TemplateBindingBorderThickness}"
Background="{TemplateBindingBackground}"
SnapsToDevicePixels="True">
<ScrollViewerFocusable="False"Padding="{TemplateBindingPadding}">
<ItemsPresenterSnapsToDevicePixels="{TemplateBindingSnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<TriggerProperty="IsGrouping"Value="True">
<SetterProperty="ScrollViewer.CanContentScroll"Value="False"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Stylex:Key="PageListBoxItemStyleKey"
TargetType="{x:TypeListBoxItem}"
BasedOn="{StaticResourceControlBasicStyle}">
<SetterProperty="MinWidth"Value="32"/>
<SetterProperty="Cursor"Value="Hand"/>
<SetterProperty="HorizontalContentAlignment"Value="Center"/>
<SetterProperty="VerticalContentAlignment"Value="Center"/>
<SetterProperty="helpers:ElementHelper.CornerRadius"Value="3"/>
<SetterProperty="BorderThickness"Value="1"/>
<SetterProperty="Padding"Value="5,0"/>
<SetterProperty="Margin"Value="3,0"/>
<SetterProperty="Background"Value="{DynamicResourceBackgroundSolidColorBrush}"/>
<SetterProperty="BorderBrush"Value="{DynamicResourceBaseSolidColorBrush}"/>
<SetterProperty="Template">
<Setter.Value>
<ControlTemplateTargetType="{x:TypeListBoxItem}">
<BorderSnapsToDevicePixels="True"
Background="{TemplateBindingBackground}"
BorderThickness="{TemplateBindingBorderThickness}"
BorderBrush="{TemplateBindingBorderBrush}"
Padding="{TemplateBindingPadding}"
CornerRadius="{BindingPath=(helpers:ElementHelper.CornerRadius),RelativeSource={RelativeSourceTemplatedParent}}">
<ContentPresenterx:Name="PART_ContentPresenter"
HorizontalAlignment="{TemplateBindingHorizontalContentAlignment}"
VerticalAlignment="{TemplateBindingVerticalContentAlignment}"
RecognizesAccessKey="True"
TextElement.Foreground="{TemplateBindingForeground}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTriggerBinding="{Binding.}"Value="">
<SetterProperty="IsEnabled"Value="False"/>
<SetterProperty="FontWeight"Value="Bold"/>
</DataTrigger>
<TriggerProperty="IsMouseover"Value="True">
<SetterProperty="BorderBrush"Value="{DynamicResourceDefaultBorderBrushSolidColorBrush}"/>
<SetterProperty="Background"Value="{DynamicResourceDefaultBackgroundSolidColorBrush}"/>
<SetterProperty="Foreground"Value="{DynamicResourcePrimaryNormalSolidColorBrush}"/>
</Trigger>
<TriggerProperty="IsSelected"Value="True">
<SetterProperty="Background"Value="{DynamicResourcePrimaryPressedSolidColorBrush}"/>
<SetterProperty="TextElement.Foreground"Value="{DynamicResourceWindowForegroundColorBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplatex:Key="LitePagerControlTemplate"TargetType="{x:Typecontrols:Pagination}">
<BorderBackground="{TemplateBindingBackground}"
BorderBrush="{TemplateBindingBorderBrush}"
BorderThickness="{TemplateBindingBorderThickness}"
Padding="{TemplateBindingPadding}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="10"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="10"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="5"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="5"/>
<ColumnDefinitionWidth="Auto"/>
</Grid.ColumnDefinitions>
<TextblockVerticalAlignment="Center"
Text="{BindingCount,StringFormat=共{0}条,RelativeSource={RelativeSourceTemplatedParent}}"/>
<TextBoxGrid.Column="2"x:Name="PART_CountPerPageTextBox"
TextAlignment="Center"VerticalContentAlignment="Center"
Width="60"MinWidth="0"
input:InputMethod.IsInputMethodEnabled="False"/>
<TextBlockGrid.Column="3"Text="条/页"VerticalAlignment="Center"/>
<ButtonGrid.Column="5"
Command="{x:Staticcontrols:Pagination.PrevCommand}">
<PathWidth="7"Height="10"Stretch="Fill"
Fill="{BindingForeground,RelativeSource={RelativeSourceAncestorType=Button}}"
Data="{StaticResourcePathPrevious}"/>
</Button>
<TextBoxGrid.Column="7"x:Name="PART_JumpPageTextBox"
TextAlignment="Center"
VerticalContentAlignment="Center"
Width="60"MinWidth="0">
<TextBox.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBindingStringFormat="{}{0}/{1}">
<BindingPath="Current"RelativeSource="{RelativeSourceTemplatedParent}"/>
<BindingPath="PageCount"RelativeSource="{RelativeSourceTemplatedParent}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
<ButtonGrid.Column="9"
Command="{x:Staticcontrols:Pagination.NextCommand}">
<PathWidth="7"Height="10"Stretch="Fill"
Fill="{BindingForeground,RelativeSource={RelativeSourceAncestorType=Button}}"
Data="{StaticResourcePathNext}"/>
</Button>
</Grid>
</Border>
</ControlTemplate>
<StyleTargetType="{x:Typecontrols:Pagination}"
BasedOn="{StaticResourceControlBasicStyle}">
<SetterProperty="Template">
<Setter.Value>
<ControlTemplateTargetType="{x:Typecontrols:Pagination}">
<BorderBackground="{TemplateBindingBackground}"
BorderBrush="{TemplateBindingBorderBrush}"
BorderThickness="{TemplateBindingBorderThickness}"
Padding="{TemplateBindingPadding}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="*"/>
<ColumnDefinitionWidth="Auto"/>
<ColumnDefinitionWidth="Auto"/>
</Grid.ColumnDefinitions>
<TextBlockMargin="0,0,15,0"VerticalAlignment="Center"
Text="{BindingCount,StringFormat=共{0}条,RelativeSource={RelativeSourceTemplatedParent}}"/>
<StackPanelGrid.Column="1"Orientation="Horizontal"Margin="0,0,15,0">
<TextBlockText="每页"VerticalAlignment="Center"/>
<TextBoxx:Name="PART_CountPerPageTextBox"
TextAlignment="Center"Width="60"
MinWidth="0"VerticalContentAlignment="Center"
FontSize="{TemplateBindingFontSize}"
input:InputMethod.IsInputMethodEnabled="False"/>
<TextBlockText="条"VerticalAlignment="Center"/>
</StackPanel>
<ButtonGrid.Column="2"
Command="{x:Staticcontrols:Pagination.PrevCommand}">
<PathWidth="7"Height="10"Stretch="Fill"
Fill="{BindingForeground,RelativeSource={RelativeSourceAncestorType=Button}}"
Data="{StaticResourcePathPrevious}"/>
</Button>
<ListBoxx:Name="PART_ListBox"Grid.Column="3"
SelectedIndex="0"Margin="5,0"
ItemsSource="{TemplateBindingPages}"
Style="{StaticResourcePageListBoxStyleKey}"
ItemContainerStyle="{StaticResourcePageListBoxItemStyleKey}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGridRows="1"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<ButtonGrid.Column="4"
Command="{x:Staticcontrols:Pagination.NextCommand}">
<PathWidth="7"Height="10"Stretch="Fill"
Fill="{BindingForeground,RelativeSource={RelativeSourceAncestorType=Button}}"
Data="{StaticResourcePathNext}"/>
</Button>
<StackPanelGrid.Column="5"Orientation="Horizontal">
<TextBlockText="前往"VerticalAlignment="Center"/>
<TextBoxx:Name="PART_JumpPageTextBox"
TextAlignment="Center"
ContextMenu="{x:Null}"
Width="60"VerticalContentAlignment="Center"
MinWidth="0"
FontSize="{TemplateBindingFontSize}"/>
<TextBlockText="页"VerticalAlignment="Center"/>
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<TriggerProperty="IsLite"Value="true">
<SetterProperty="Template"Value="{StaticResourceLitePagerControlTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
3) 创建PaginationExampleVM.cs如下:
usingSystem.Collections.Generic;
usingSystem.Collections.ObjectModel;
usingSystem.Linq;
namespaceWPFDevelopers.Samples.ViewModels
{
publicclassPaginationExampleVM:ViewModelBase
{
privateList<int>_sourceList=newList<int>();
publicPaginationExampleVM()
{
_sourceList.AddRange(Enumerable.Range(1,300));
Count=300;
CurrentPageChanged();
}
publicObservableCollection<int>PaginationCollection{get;set;}=newObservableCollection<int>();
privateint_count;
publicintCount
{
get{return_count;}
set{_count=value;this.NotifyPropertyChange("Count");CurrentPageChanged();}
}
privateint_countPerPage=10;
publicintCountPerPage
{
get{return_countPerPage;}
set{_countPerPage=value;this.NotifyPropertyChange("CountPerPage");CurrentPageChanged();}
}
privateint_current=1;
publicintCurrent
{
get{return_current;}
set{_current=value;this.NotifyPropertyChange("Current");CurrentPageChanged();}
}
privatevoidCurrentPageChanged()
{
PaginationCollection.Clear();
foreach(variin_sourceList.Skip((Current-1)*CountPerPage).Take(CountPerPage))
{
PaginationCollection.Add(i);
}
}
}
}
4) 使用 PaginationExample.xaml 如下:
<UserControlx: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>
<StyleTargetType="{x:TypeTextBlock}">
<SetterProperty="Foreground"Value="{DynamicResourcePrimaryTextSolidColorBrush}"/>
<SetterProperty="FontSize"Value="{StaticResourceNormalFontSize}"/>
<SetterProperty="VerticalAlignment"Value="Center"/>
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinitionHeight="50"/>
<RowDefinition/>
<RowDefinitionHeight="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinitionWidth="2*"/>
<ColumnDefinitionWidth="30"/>
<ColumnDefinitionWidth="*"/>
</Grid.ColumnDefinitions>
<TextBlockText="正常模式分页"HorizontalAlignment="Center"VerticalAlignment="Center"/>
<TextBlockGrid.Column="2"Text="精简模式分页"HorizontalAlignment="Center"VerticalAlignment="Center"/>
<ListBoxGrid.Row="1"Grid.Column="0"ItemsSource="{BindingNormalPaginationViewModel.PaginationCollection}"Margin="20,0,0,0"/>
<ListBoxGrid.Row="1"Grid.Column="2"ItemsSource="{BindingLitePaginationViewModel.PaginationCollection}"Margin="0,0,20,0"/>
<wpfdev:PaginationGrid.Row="2"Grid.Column="0"IsLite="False"Margin="20,0,0,0"
Count="{BindingNormalPaginationViewModel.Count,Mode=TwoWay}"
CountPerPage="{BindingNormalPaginationViewModel.CountPerPage,Mode=TwoWay}"
Current="{BindingNormalPaginationViewModel.Current,Mode=Twohttp://www.cppcns.comWay}"/>
<wpfdev:PaginationGrid.Row="2"Grid.Column="2"IsLite="true"Margin="0,0,20,0"
Count="{BindingLitePaginationViewModel.Count,Mode=TwoWay}"
CountPerPage="{BindingLitePaginationViewModel.CountPerPage,Mode=TwoWay}"
Current="{BindingLitePaginationViewModel.Current,Mode=TwoWay}"/>
</Grid>
</UserControl>
5) 使用PaginationExample.xaml.cs如下:
usingSystem.Windows.Controls;
usingWPFDevelopers.Samples.ViewModels;
namespaceWPFDevelopers.Samples.ExampleViews
{
///<summary>
///PaginationExample.xaml的交互逻辑
///</summary>
publicpartialclassPaginationExample:UserControl
{
publicPaginationExampleVMNormalPaginationViewModel{get;set;}=newPaginationExampleVM();
publicPaginationExampleVMLitePaginationViewModel{get;set;}=newPaginationExampleVM();
publicPaginationExample()
{
InitializeComponent();
this.DataContext=this;
}
}
}
实现效果

以上就是WPF实现列表分页控件的示例代码的详细内容,更多关于WPF列表分页控件的资料请关注我们其它相关文章!










