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

文章目录

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

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

效果预览

WPF定制实现自己的分页控件并配合DataGrid使用_第1张图片

实现功能

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

属性说明

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

事件说明

事件 说明
GoButtonOnClick GO按钮绑定事件
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
{
    /// 
    /// DakaPathPageControl.xaml 的交互逻辑
    /// 
    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文件。接下来在需要使用分页控件的页面引用自己的控件库,即可在页面设计器中实时预览该控件的使用效果。
WPF定制实现自己的分页控件并配合DataGrid使用_第2张图片
配合MahApps.Metro,使用效果如下
WPF定制实现自己的分页控件并配合DataGrid使用_第3张图片

Tip

自己注册属性,可以通过输入propdp,然后按两次Tab键自动生成相关代码
WPF定制实现自己的分页控件并配合DataGrid使用_第4张图片

你可能感兴趣的:(C#,WPF,分页控件)