900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > WPF定制实现自己的分页控件并配合DataGrid使用

WPF定制实现自己的分页控件并配合DataGrid使用

时间:2019-02-26 02:47:21

相关推荐

WPF定制实现自己的分页控件并配合DataGrid使用

文章目录

效果预览实现功能属性说明事件说明相关代码按钮事件参数:PageButtonEventArgs.cs分页控件:DakaPathPageControl.xaml使用Tip

因为项目需求,在使用WPF原生DataGrid时,需要使用到分页控件,所以自定义了分页控件

效果预览

实现功能

每页加载数据量选择,PageSize属性根据输入页码进行跳转GO上一页下一页刷新四个按钮可以自定义单击事件可以设定边框、圆角边框可以隐藏不想要显示的子控件

属性说明

事件说明

相关代码

按钮事件参数:PageButtonEventArgs.cs

using System.Windows;namespace DakaPath.Wpf.Control.EventArgs{public class PageButtonEventArgs : RoutedEventArgs{public PageButtonEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source){}public int PageIndex{set;get;}}}

分页控件:DakaPathPageControl.xaml

新建一个用户控件,并命名为DakaPathPageControl

using System;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using DakaPath.Wpf.Control.EventArgs;namespace DakaPath.Wpf.Control{/// <summary>/// DakaPathPageControl.xaml 的交互逻辑/// </summary>public partial class DakaPathPageControl : UserControl{public DakaPathPageControl(){InitializeComponent();}public Visibility PageIndexVisibility{get {return (Visibility)GetValue(PageIndexVisibilityProperty); }set {SetValue(PageIndexVisibilityProperty, value); }}public static readonly DependencyProperty PageIndexVisibilityProperty =DependencyProperty.Register("PageIndexVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility PageCountVisibility{get {return (Visibility)GetValue(PageCountVisibilityProperty); }set {SetValue(PageCountVisibilityProperty, value); }}public static readonly DependencyProperty PageCountVisibilityProperty =DependencyProperty.Register("PageCountVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility PageNumSelectVisibility{get {return (Visibility)GetValue(PageNumSelectVisibilityProperty); }set {SetValue(PageNumSelectVisibilityProperty, value); }}public static readonly DependencyProperty PageNumSelectVisibilityProperty =DependencyProperty.Register("PageNumSelectVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility DataCountVisibility{get {return (Visibility)GetValue(DataCountVisibilityProperty); }set {SetValue(DataCountVisibilityProperty, value); }}public static readonly DependencyProperty DataCountVisibilityProperty =DependencyProperty.Register("DataCountVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public Visibility JumpVisibility{get {return (Visibility)GetValue(JumpVisibilityProperty); }set {SetValue(JumpVisibilityProperty, value); }}public static readonly DependencyProperty JumpVisibilityProperty =DependencyProperty.Register("JumpVisibility", typeof(Visibility), typeof(DakaPathPageControl), new PropertyMetadata(Visibility.Visible));public int PageIndex{get {return (int)GetValue(PageIndexProperty); }set {SetValue(PageIndexProperty, value); }}public static readonly DependencyProperty PageIndexProperty =DependencyProperty.Register("PageIndex", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(0));public int PageSize{get {return (int)GetValue(PageSizeProperty); }//set { SetValue(PageSizeProperty, value); }}public static readonly DependencyProperty PageSizeProperty =DependencyProperty.Register("PageSize", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(50));public int PageCount{get {return (int)GetValue(PageCountProperty); }set {SetValue(PageCountProperty, value); }}public static readonly DependencyProperty PageCountProperty =DependencyProperty.Register("PageCount", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(0));public int DataCount{get {return (int)GetValue(DataCountProperty); }set{SetValue(DataCountProperty, value);if (value > 0){SetValue(DataCountProperty, value);}else{SetValue(DataCountProperty, 0);}}}public static readonly DependencyProperty DataCountProperty =DependencyProperty.Register("DataCount", typeof(int), typeof(DakaPathPageControl), new PropertyMetadata(0));public string JumpNum{get{return (string)GetValue(JumpNumProperty);}set{SetValue(JumpNumProperty, value);}}public static readonly DependencyProperty JumpNumProperty =DependencyProperty.Register("JumpNum", typeof(string), typeof(DakaPathPageControl), null);public Brush ButtonBackground{get {return (Brush)GetValue(ButtonBackgroundProperty); }set {SetValue(ButtonBackgroundProperty, value); }}public static readonly DependencyProperty ButtonBackgroundProperty =DependencyProperty.Register("ButtonBackground", typeof(Brush), typeof(DakaPathPageControl), new PropertyMetadata((Brush)(new BrushConverter()).ConvertFromString("#337AB7")));public Brush ButtonTextForeground{get {return (Brush)GetValue(ButtonTextForegroundProperty); }set {SetValue(ButtonTextForegroundProperty, value); }}public static readonly DependencyProperty ButtonTextForegroundProperty =DependencyProperty.Register("ButtonTextForeground", typeof(Brush), typeof(DakaPathPageControl), new PropertyMetadata(Brushes.White));public new Brush BorderBrush{get {return (Brush)GetValue(BorderBrushProperty); }set {SetValue(BorderBrushProperty, value); }}public static readonly new DependencyProperty BorderBrushProperty =DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(DakaPathPageControl));public new Brush Background{get {return (Brush)GetValue(BackgroundProperty); }set {SetValue(BackgroundProperty, value); }}public static readonly new DependencyProperty BackgroundProperty =DependencyProperty.Register("Background", typeof(Brush), typeof(DakaPathPageControl));public new Thickness BorderThickness{get {return (Thickness)GetValue(BorderThicknessProperty); }set {SetValue(BorderThicknessProperty, value); }}public static readonly new DependencyProperty BorderThicknessProperty =DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(DakaPathPageControl));public float CornerRadius{get {return (float)GetValue(CornerRadiusProperty); }set {SetValue(CornerRadiusProperty, value); }}public static readonly DependencyProperty CornerRadiusProperty =DependencyProperty.Register("CornerRadius", typeof(float), typeof(DakaPathPageControl));public event RoutedEventHandler NextPageButtonOnClick{add{this.AddHandler(NextPageButtonOnClickEvent, value);}remove {this.RemoveHandler(NextPageButtonOnClickEvent, value); }}public static readonly RoutedEvent NextPageButtonOnClickEvent = EventManager.RegisterRoutedEvent("NextButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_next_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(NextPageButtonOnClickEvent, this);if (PageCount < 0){PageCount = 0;}if (PageIndex < PageCount){PageIndex++;}else{PageIndex = PageCount;}args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}public event RoutedEventHandler PreviousPageButtonOnClick{add{this.AddHandler(PreviousPageButtonOnClickEvent, value);}remove {this.RemoveHandler(PreviousPageButtonOnClickEvent, value); }}public static readonly RoutedEvent PreviousPageButtonOnClickEvent =EventManager.RegisterRoutedEvent("PreviousButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_previous_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(PreviousPageButtonOnClickEvent, this);if (PageIndex > 0){PageIndex--;}args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}public event RoutedEventHandler GoPageButtonOnClick{add{this.AddHandler(GoPageButtonOnClickEvent, value);}remove {this.RemoveHandler(GoPageButtonOnClickEvent, value); }}public static readonly RoutedEvent GoPageButtonOnClickEvent =EventManager.RegisterRoutedEvent("GoButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_go_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(GoPageButtonOnClickEvent, this);int inputPageIndex = 0;int.TryParse(JumpNum,out inputPageIndex);if (PageCount < 0){PageCount = 0;}if (inputPageIndex <= PageCount && inputPageIndex > 0){PageIndex = inputPageIndex;}args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}public event RoutedEventHandler RefreshPageButtonOnClick{add{this.AddHandler(RefreshPageButtonOnClickEvent, value);}remove {this.RemoveHandler(RefreshPageButtonOnClickEvent, value); }}public static readonly RoutedEvent RefreshPageButtonOnClickEvent =EventManager.RegisterRoutedEvent("RefreshButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DakaPathPageControl));private void btn_refresh_Click(object sender, RoutedEventArgs e){PageButtonEventArgs args = new PageButtonEventArgs(RefreshPageButtonOnClickEvent, this);args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}private void cmb_pagesizeset_SelectionChanged(object sender, SelectionChangedEventArgs e){var cbi = e.AddedItems[0] as ComboBoxItem;if (cbi != null){int size = Int32.Parse(cbi.Content as String);SetValue(PageSizeProperty, size);PageButtonEventArgs args = new PageButtonEventArgs(RefreshPageButtonOnClickEvent, this);args.PageIndex = PageIndex;RaiseEvent(args);GC.Collect();}}}}

使用

将自己的控件项目生成为dll文件,并在需要使用分页控件的项目中引用该dll文件。接下来在需要使用分页控件的页面引用自己的控件库,即可在页面设计器中实时预览该控件的使用效果。

配合MahApps.Metro,使用效果如下

Tip

自己注册属性,可以通过输入propdp,然后按两次Tab键自动生成相关代码

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
WPF(5)WPF之DataGrid控件

WPF(5)WPF之DataGrid控件

2022-11-04

WPF Datagrid控件

WPF Datagrid控件

2021-03-15

WPF中DataGrid控件

WPF中DataGrid控件

2020-07-22

WPF DataGrid控件样式

WPF DataGrid控件样式

2021-06-05