当前位置: 首页 > article >正文

WPF自定义翻页控件

XAML文件如下:

<UserControl
    x:Class="CTMVVMDemo.View.UserControls.DataPager"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:CTMVVMDemo.View.UserControls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Name="userControl"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid>
        <!--
            一个视图数据源一般情况来自两处:
            1。ViewModel
            2。.cs文件
        -->

        <StackPanel
            HorizontalAlignment="Right"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <TextBlock Text="共" />
            <TextBlock Margin="4,0" Text="{Binding TotalCount, ElementName=userControl}" />
            <TextBlock Text="条记录,每页" />
            <ComboBox
                Grid.Column="0"
                Width="60"
                Height="24"
                Margin="5,0,0,0"
                Padding="0"
                VerticalAlignment="Center"
                HorizontalContentAlignment="Center"
                VerticalContentAlignment="Center"
                Cursor="Hand"
                ItemsSource="{Binding ElementName=userControl, Path=PageSizes}"
                SelectedItem="{Binding PageSize, ElementName=userControl}" />
            <TextBlock Text="条,第" />
            <TextBlock Margin="4,0,2,0" Text="{Binding PageIndex, ElementName=userControl}" />
            <TextBlock Text="/" />
            <TextBlock Margin="2,0,4,0" Text="{Binding PageCount, ElementName=userControl}" />
            <TextBlock Text="页" />
            <Button
                x:Name="btnFirst"
                Margin="10,0,0,0"
                VerticalAlignment="Center"
                Click="btnFirst_Click"
                Content="首页"
                FontSize="14"
                IsEnabled="{Binding CanGoFirstOrPrev, ElementName=userControl}" />
            <Button
                x:Name="btnPrev"
                Margin="10,0,0,0"
                VerticalAlignment="Center"
                Click="btnPrev_Click"
                Content="上一页"
                FontSize="14"
                IsEnabled="{Binding CanGoFirstOrPrev, ElementName=userControl}" />
            <Button
                x:Name="btnNext"
                Margin="10,0,10,0"
                VerticalAlignment="Center"
                Click="btnNext_Click"
                Content="下一页"
                FontSize="14"
                IsEnabled="{Binding CanGoLastOrNext, ElementName=userControl}" />

            <Button
                x:Name="btnLast"
                Margin="10,0,10,0"
                VerticalAlignment="Center"
                Click="btnLast_Click"
                Content="末页"
                FontSize="14"
                IsEnabled="{Binding CanGoLastOrNext, ElementName=userControl}" />
        </StackPanel>

    </Grid>
</UserControl>

依赖属性的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CTMVVMDemo.View.UserControls
{
    public partial class DataPager : UserControl, INotifyPropertyChanged
    {
        public DataPager()
        {
            InitializeComponent();
        }

        #region 路由事件
        public static readonly RoutedEvent PageChangedEvent = EventManager.RegisterRoutedEvent(
            "PageChanged",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(DataPager));

        public event RoutedEventHandler PageChanged
        {
            add { AddHandler(PageChangedEvent, value); }
            remove { RemoveHandler(PageChangedEvent, value); }
        }
        #endregion

        #region 依赖属性

        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            get { return (int)GetValue(PageIndexProperty); }
            set { SetValue(PageIndexProperty, value); }
        }
        public static readonly DependencyProperty PageIndexProperty =
            DependencyProperty.Register(
                "PageIndex",
                typeof(int),
                typeof(DataPager),
                new UIPropertyMetadata(1, (sender, e) =>
                {
                    var dp = sender as DataPager;
                    if (dp == null) return;
                    dp.ChangeNavigationButtonState();
                }));

        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }
        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register(
                "PageSize",
                typeof(int),
                typeof(DataPager),
                new UIPropertyMetadata(10, (sender, e) =>
                {
                    var dp = sender as DataPager;
                    if (dp == null) return;
                    dp.InitData();
                    dp.OnPageChanging(1);
                    dp.ChangeNavigationButtonState();
                }));

        /// <summary>
        /// 总记录数量
        /// </summary>
        public int TotalCount
        {
            get { return (int)GetValue(TotalCountProperty); }
            set
            {
                SetValue(TotalCountProperty, value);
            }
        }
        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register(
                "TotalCount",
                typeof(int),
                typeof(DataPager),
                new UIPropertyMetadata(0, (sender, e) =>
                {
                    var dp = sender as DataPager;
                    if (dp == null) return;
                    dp.InitData();
                    dp.ChangeNavigationButtonState();
                }));

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get { return (int)GetValue(PageCountProperty); }
            private set { SetValue(PageCountProperty, value); }
        }
        public static readonly DependencyProperty PageCountProperty =
            DependencyProperty.Register("PageCount", typeof(int), typeof(DataPager), new UIPropertyMetadata(1));


        public List<int> PageSizes
        {
            get { return (List<int>)GetValue(PageSizesProperty); }
            set { SetValue(PageSizesProperty, value); }
        }

        public static readonly DependencyProperty PageSizesProperty =
            DependencyProperty.Register(
                "PageSizes",
                typeof(List<int>),
                typeof(DataPager),
                new PropertyMetadata(new List<int>() { 1, 10, 20, 30, 40, 50 })
                );

        #endregion 依赖属性

        #region 初始化
        /// <summary>
        /// 初始化数据
        /// </summary>
        void InitData()
        {
            // 根据记录总数计算总页数
            if (TotalCount == 0)
                PageCount = 1;
            else
                PageCount = TotalCount % PageSize > 0 ? (TotalCount / PageSize) + 1 : TotalCount / PageSize;

            // 防止非法数据
            if (PageIndex < 1)
                PageIndex = 1;

            if (PageIndex > PageCount)
                PageIndex = PageCount;

            if (PageSize < 1)
                PageSize = 10;
        }
        #endregion

        #region 按钮逻辑
        /// <summary>
        /// 点击首页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            OnPageChanging(1);
        }

        /// <summary>
        /// 点击上一页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            OnPageChanging(PageIndex - 1);
        }

        /// <summary>
        /// 点击下一页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            OnPageChanging(PageIndex + 1);
        }

        /// <summary>
        /// 点击末页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            OnPageChanging(PageCount);
        }

        private void TxtPageIndex_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9]+");
            e.Handled = re.IsMatch(e.Text);
        }

        /// <summary>
        /// 点击跳转按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGoTo_Click(object sender, RoutedEventArgs e)
        {
            int pageIndex = 1;
            try
            {
                //pageIndex = Convert.ToInt32(txtPageIndex.Text);
            }
            catch
            {
            }
            finally
            {
                OnPageChanging(pageIndex);
            }
        }
        #endregion

        /// <summary>
        /// 页码更改
        /// </summary>
        /// <param name="pageIndex"></param>
        private void OnPageChanging(int pageIndex)
        {
            if (pageIndex < 1) pageIndex = 1;
            if (pageIndex > PageCount) pageIndex = PageCount;

            var eventArgs = new RoutedEventArgs(PageChangedEvent, this);
            PageIndex = pageIndex;
            RaiseEvent(eventArgs);
        }

        #region 控制按钮状态
        /// <summary>
        /// 是否可以点击首页和上一页按钮
        /// </summary>
        public bool CanGoFirstOrPrev
        {
            get
            {
                if (PageIndex <= 1) return false;
                return true;
            }
        }

        /// <summary>
        /// 是否可以点击最后页和下一页按钮
        /// </summary>
        public bool CanGoLastOrNext
        {
            get
            {
                if (PageIndex >= PageCount) return false;
                return true;
            }
        }

        /// <summary>
        /// 通知导航按钮(首页,上一页,下一页,末页)状态的更改
        /// </summary>
        void ChangeNavigationButtonState()
        {
            OnPropertyChanged("CanGoFirstOrPrev");
            OnPropertyChanged("CanGoLastOrNext");
        }

        #endregion

        #region INotifyPropertyChanged成员
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion INotifyPropertyChanged成员

        
    }
}

效果图:


http://www.kler.cn/a/387368.html

相关文章:

  • Java中的注解:如何自定义注解并实现功能
  • 12 USART串口通讯
  • [c语言日寄]精英怪:三子棋(tic-tac-toe)3命慢通[附免费源码]
  • Open FPV VTX开源之betaflight配置
  • 如何制作一个高质量的 Dockerfile 镜像:从入门到实践
  • 华为2024嵌入式研发面试题
  • 简单的TCP程序
  • RK3568笔记1:BootRom
  • 【Linux 29】传输层协议 - UDP
  • 28-在CARLA包中获取地图
  • vue之vant上传图片
  • 数据结构-归并排序笔记
  • Java 连接操作 MySQL 数据库(增删查改操作)
  • 文献阅读 | Nature Methods:使用 STAMP 对空间转录组进行可解释的空间感知降维
  • LLMs在供应链投毒检测中的应用
  • 植物明星大乱斗1
  • 利用AI工具进行论文数据收集
  • 了解GPT大模型,读这本书就够了!(文末送书)
  • 【模块化大作战】Webpack如何搞定CommonJS与ES6混战(1-3)
  • 【网络】深入理解 HTTPS:确保数据传输安全的核心协议
  • 今天要重新认识下注解@RequestBody
  • IDEA构建JavaWeb项目,并通过Tomcat成功运行
  • 【快速入门】Kafka的安装部署
  • 关于QUERY_ALL_PACKAGES权限导致Google下架apk
  • LLM大模型学习精华系列:VLLM性能优化部署实践——全面加速从推理到部署的流程
  • 【ESP】一小时速通入门笔记