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

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

效果预览

在这里插入图片描述

实现功能

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

属性说明

属性说明
PageIndexVisibility控制[第 x 页]文本框的显示
PageCountVisibility控制[共 x 页]文本框的显示
DataCountVisibility控制[共 x 条记录]文本框的显示
PageNumSelectVisibility控制每页显示数量选择框的显示
JumpVisibility控制与页面跳转相关控件的显示
PageIndex当前页码值
PageSize只读,每页显示数量
PageCount共多少页
DataCount共多少条记录
JumpNum跳转到的页码值
ButtonBackground设定按钮背景色
ButtonTextForeground设定按钮文字颜色
BorderBrush设定控件边框色
BorderThickness设定控件边框
Background设定控件背景色
CornerRadius设定控件边框圆角值

事件说明

事件说明
GoButtonOnClickGO按钮绑定事件
PreviousButtonOnClick上一页按钮绑定事件
NextButtonOnClick下一页按钮绑定事件
RefreshButtonOnClick刷新按钮绑定事件

相关代码

按钮事件参数: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键自动生成相关代码
在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0564丶Kang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值