首页 > 其他分享 >自定义分页控件

自定义分页控件

时间:2024-05-22 09:43:00浏览次数:20  
标签:控件 分页 自定义 int void private pagingControl public CurrentPageIndex

自定义分页控件

tip: 该控件的样式用的是materialDesign库,需要下载Nuget包

Code

  • Xaml
<UserControl
    x:Class="TestTool.CustomControls.PagingControl"
    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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="pagingControl"
    TextElement.FontSize="14"
    mc:Ignorable="d">
    <UserControl.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource MaterialDesignIconButton}">
            <Setter Property="Width" Value="26" />
            <Setter Property="Height" Value="26" />
        </Style>
    </UserControl.Resources>
    <StackPanel DataContext="{Binding ElementName=pagingControl}" Orientation="Horizontal">
        <Button
            Click="Button_FirstPage_Click"
            ToolTip="首页">
            <materialDesign:PackIcon
                Width="24"
                Height="24"
                VerticalAlignment="Center"
                Kind="PageFirst" />
        </Button>
        <Button
            Margin="10,0,10,0"
            Click="Button_Previous_Click"
            ToolTip="上一页">
            <materialDesign:PackIcon
                Width="20"
                Height="20"
                VerticalAlignment="Center"
                Kind="ArrowLeftCircle" />
        </Button>
        <TextBlock VerticalAlignment="Center" Text="{Binding CurrentPageIndex}" />
        <TextBlock VerticalAlignment="Center">/</TextBlock>
        <TextBlock VerticalAlignment="Center" Text="{Binding TotalPageCount}" />
        <Button
            Width="26"
            Height="26"
            Margin="10,0,0,0"
            Click="Button_Next_Click"
            ToolTip="下一页">
            <materialDesign:PackIcon
                Width="20"
                Height="20"
                VerticalAlignment="Center"
                Kind="ArrowRightCircle" />
        </Button>
        <Button
            Margin="10,0,0,0"
            Click="Button_Last_Click"
            ToolTip="尾页">
            <materialDesign:PackIcon
                Width="24"
                Height="24"
                VerticalAlignment="Center"
                Kind="PageLast" />
        </Button>
        <TextBlock Margin="20,0,0,0" VerticalAlignment="Center">共</TextBlock>
        <TextBlock
            Margin="5,0,0,0"
            VerticalAlignment="Center"
            Text="{Binding TotalCount}" />
        <TextBlock Margin="5,0,0,0" VerticalAlignment="Center">条记录</TextBlock>
        <Separator
            Height="14"
            Margin="10,0,0,0"
            BorderThickness="0.5"
            Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
        <TextBlock Margin="10,0,0,0" VerticalAlignment="Center">每页数量</TextBlock>
        <ComboBox
            MinWidth="50"
            Margin="5,0,0,0"
            VerticalContentAlignment="Center"
            ItemsSource="{Binding PagesSizes}"
            SelectedIndex="0"
            SelectedItem="{Binding CurrentPageSize}"
            SelectionChanged="ComboBox_PageSize_SelectionChanged" />
        <Separator
            Height="14"
            Margin="10,0,0,0"
            BorderThickness="0.5"
            Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
        <TextBlock Margin="10,0,0,0" VerticalAlignment="Center">跳转到第</TextBlock>
        <TextBox
            Width="50"
            Margin="5,0,5,0"
            VerticalAlignment="Center"
            HorizontalContentAlignment="Center"
            Text="{Binding GotoPageIndex}" />
        <TextBlock VerticalAlignment="Center">页</TextBlock>
        <Button
            Margin="5,0,0,0"
            Click="Button_Goto_Click"
            ToolTip="跳转">
            <materialDesign:PackIcon
                Width="24"
                Height="24"
                VerticalAlignment="Center"
                Kind="ArrowRightBold" />
        </Button>
    </StackPanel>
</UserControl>
  • C#
public partial class PagingControl : UserControl, INotifyPropertyChanged
{
    // 用于防抖、节流
    private readonly ConcurrentQueue<DateTime> _operationTime = new();

    //用于防抖、节流,拦截两次时间少于1秒的操作
    private readonly TimeSpan _ignoreOperationLessThen = TimeSpan.FromSeconds(1);

    public event EventHandler CanExecuteChanged;
    public event PropertyChangedEventHandler? PropertyChanged;

    #region 普通属性
    /// <summary>
    /// 能否执行状态
    /// </summary>
    public bool CanExecute => QueryCommand?.CanExecute(null) ?? true;
    //在load后是否立即触发命令和路由事件
    public bool LoadedTrigger { get; set; } = false;
    public ObservableCollection<int> PagesSizes { get; set; } = [15, 30, 60, 200, 500, 2000];

    /// <summary>
    /// 总页数
    /// </summary>
    private int _totalPageCount;
    public int TotalPageCount
    {
        get => _totalPageCount;
        set
        {
            if (_totalPageCount != value)
            {
                _totalPageCount = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TotalPageCount)));
            }
        }
    }

    /// <summary>
    /// 跳转页码
    /// </summary>
    private int _gotoPageIndex;
    public int GotoPageIndex
    {
        get => _gotoPageIndex;
        set
        {
            if (_gotoPageIndex != value)
            {
                _gotoPageIndex = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(GotoPageIndex)));
            }
        }
    }

    /// <summary>
    /// 当前页大小
    /// </summary>
    private int _currentPageSize = 15;
    public int CurrentPageSize
    {
        get => _currentPageSize;
        set
        {
            if (_currentPageSize != value)
            {
                _currentPageSize = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentPageSize)));
            }
        }
    }

    /// <summary>
    /// 当前页码
    /// </summary>
    private int _currentPageIndex;
    public int CurrentPageIndex
    {
        get => _currentPageIndex;
        set
        {
            if (_currentPageIndex != value)
            {
                _currentPageIndex = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentPageIndex)));
            }
        }
    }

    //用于外部绑定命令
    public ICommand PageQueryCommand { get; }
    #endregion

    #region 依赖属性
    /// <summary>
    /// 总数
    /// </summary>
    public int TotalCount
    {
        get { return (int)GetValue(TotalCountProperty); }
        set { SetValue(TotalCountProperty, value); }
    }
    public ICommand QueryCommand
    {
        get { return (ICommand)GetValue(QueryCommandProperty); }
        set { SetValue(QueryCommandProperty, value); }
    }

    public static readonly DependencyProperty TotalCountProperty =
        DependencyProperty.Register("TotalCount", typeof(int), typeof(PagingControl), new FrameworkPropertyMetadata(15, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, TotalCountChangedCallback));

    public static readonly DependencyProperty QueryCommandProperty =
        DependencyProperty.Register("QueryCommand", typeof(ICommand), typeof(PagingControl), new PropertyMetadata(null, QueryCommandChangedCallback));

    #endregion

    public static readonly RoutedEvent PagingChangedEvent = EventManager.RegisterRoutedEvent("PagingChanged", RoutingStrategy.Bubble, typeof(EventHandler<PagingChangedEventArgs>), typeof(PagingControl));

    public event RoutedEventHandler PagingChanged
    {
        add => AddHandler(PagingChangedEvent, value);
        remove => RemoveHandler(PagingChangedEvent, value);
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    public PagingControl()
    {
        InitializeComponent();

        TotalCount = 0;
        GotoPageIndex = 1;
        CurrentPageIndex = 0;
        TotalPageCount = 0;

        PageQueryCommand = new PageQueryCommand(this);
    }

    static void TotalCountChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PagingControl pagingControl = (PagingControl)d;
        pagingControl.ReLoad((int)e.NewValue);
    }

    static void QueryCommandChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var command = e.NewValue as ICommand;
        if (command != null)
        {
            PagingControl pagingControl = (PagingControl)d;

            command.CanExecuteChanged -= pagingControl.Command_CanExecuteChanged;
            command.CanExecuteChanged += pagingControl.Command_CanExecuteChanged;

            if (pagingControl.LoadedTrigger)
                pagingControl.FirstPage();
        }
    }

    private void Command_CanExecuteChanged(object? sender, EventArgs e)
    {
        CanExecuteChanged?.Invoke(this, e);
        IsEnabled = QueryCommand.CanExecute(null);
    }

    #region 事件
    /// <summary>
    /// 每页大小  ComboBox 变化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ComboBox_PageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ReLoad(TotalCount);

        CurrentPageIndex = 1;
        OnPagingChanged(CurrentPageIndex);
        e.Handled = true;
    }

    /// <summary>
    /// 首页按钮点击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_FirstPage_Click(object sender, RoutedEventArgs e)
    {
        FirstPage();
        e.Handled = true;
    }

    /// <summary>
    /// 尾页按钮点击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Last_Click(object sender, RoutedEventArgs e)
    {
        LastPage();
        e.Handled = true;
    }

    /// <summary>
    /// 上一页按钮点击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Previous_Click(object sender, RoutedEventArgs e)
    {
        PreviousPage();
        e.Handled = true;
    }

    /// <summary>
    /// 下一页按钮点击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Next_Click(object sender, RoutedEventArgs e)
    {
        NextPage();
        e.Handled = true;
    }

    /// <summary>
    /// 跳转按钮点击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button_Goto_Click(object sender, RoutedEventArgs e)
    {
        GoToPage();
        e.Handled = true;
    }
    #endregion

    private void ReLoad(int totalCount)
    {
        TotalPageCount = (totalCount + CurrentPageSize - 1) / CurrentPageSize;

        if (TotalPageCount == 0)
            CurrentPageIndex = 0;
        else if (TotalPageCount != 0 && CurrentPageIndex == 0)
            CurrentPageIndex = 1;
    }

    private bool CheckCanExcetue()
    {
        if (_operationTime.TryDequeue(out var time))
        {
            if (DateTime.Now - time < _ignoreOperationLessThen)
                return false;
        }
        _operationTime.Enqueue(DateTime.Now);

        return CanExecute;
    }

    public void FirstPage()
    {
        if (!CheckCanExcetue()) return;

        CurrentPageIndex = 1;
        OnPagingChanged(CurrentPageIndex);
    }

    public void LastPage()
    {
        if (!CheckCanExcetue()) return;

        if (TotalPageCount == 0)
        {
            OnPagingChanged(1);
        }
        else
        {
            CurrentPageIndex = TotalPageCount;
            OnPagingChanged(CurrentPageIndex);
        }
    }

    public void NextPage()
    {
        if (!CheckCanExcetue()) return;

        if (CurrentPageIndex >= TotalPageCount)
        {
            OnPagingChanged(CurrentPageIndex);
        }
        else
        {
            CurrentPageIndex++;
            OnPagingChanged(CurrentPageIndex);
        }
    }

    public void PreviousPage()
    {
        if (!CheckCanExcetue()) return;

        if (CurrentPageIndex > TotalPageCount)
        {
            CurrentPageIndex = TotalPageCount;
        }

        if (CurrentPageIndex > 1)
        {
            CurrentPageIndex--;
            OnPagingChanged(CurrentPageIndex);
        }
        else
        {
            OnPagingChanged(1);
        }
    }

    public void GoToPage()
    {
        if (!CheckCanExcetue()) return;

        if (GotoPageIndex < 1 || GotoPageIndex > TotalPageCount)
            return;

        CurrentPageIndex = GotoPageIndex;
        OnPagingChanged(CurrentPageIndex);
    }

    private void OnPagingChanged(int pageIndex)
    {
        PagingChangedEventArgs pagingChangedEventArgs = new(PagingChangedEvent, this,
            new PagingInfo(pageIndex, CurrentPageSize));

        RaiseEvent(pagingChangedEventArgs);

        QueryCommand?.Execute(new PagingInfo(pageIndex, CurrentPageSize));
    }
}

相关自定义类

public record class PagingInfo(int TargetPageIndex, int PageSize);

路由事件参数

public class PagingChangedEventArgs(RoutedEvent routedEvent, object source, PagingInfo pageInfo) : RoutedEventArgs(routedEvent, source)
{
    public PagingInfo PageInfo { get; set; } = pageInfo;
}

外部绑定命令(用于MVVM模式下,需要其它地方触发分页控件命令)

public class PageQueryCommand : ICommand
{
    private readonly PagingControl _pagingControl;

    public event EventHandler? CanExecuteChanged;

    public PageQueryCommand(PagingControl pagingControl)
    {
        _pagingControl = pagingControl;
        _pagingControl.CanExecuteChanged += (o, e) => CanExecuteChanged?.Invoke(o, e);
    }

    public bool CanExecute(object? parameter)
    {
        return _pagingControl?.CanExecute ?? true;
    }

    public void Execute(object? parameter)
    {
        _pagingControl.FirstPage();
    }
}

Demo

  • 当点击分页控件按钮时会触发QueryCommand命令和路由事件,并传入相关参数,需要在查询完结果后将查询到的总数赋值给TotalCount以便更新UI显示的相关信息
  • 当需要外部按钮来激发查询命令时,可绑定分页控件的pageQueryCommand
<Button
    x:Name="buttonQuery"
    Margin="10,0,0,0"
    HorizontalAlignment="Stretch"
    Content="查询"
    Command="{Binding ElementName=pageControl, Path=PageQueryCommand}"/>
<customControls:PagingControl
    x:Name="pageControl"
    HorizontalAlignment="Right"
    DockPanel.Dock="Bottom"
    LoadedTrigger="False"
    QueryCommand="{Binding PageQueryCommand}"
    TotalCount="{Binding TotalCount}" />

 [RelayCommand]
    private async Task PageQuery(PagingInfo pagingInfo){
//通过数据库进行查询
TotalCount=查出结果数量
    }

希望能给你带来帮助 --来自.net菜鸡粉丝的祝愿

标签:控件,分页,自定义,int,void,private,pagingControl,public,CurrentPageIndex
From: https://www.cnblogs.com/yinyuessh/p/18205496

相关文章

  • 自定义可移动点二维坐标轴控件
    自定义可移动点二维坐标轴控件目录路由参数坐标轴控件定义Demo路由参数X_YResultCollection为当前X轴对应Y轴值存储字典publicclassResultCollectionChangedEventArgs(RoutedEventroutedEvent,objectsource,IDictionary<double,double>resultCollection):Route......
  • GridLayout 等控件来完成多行按钮操作
     第一步,在布局文件中添加一个GridLayout控件,设置它的行列数和间距等属性,例如:<GridLayoutandroid:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="4"andr......
  • form-create-designer中怎么扩展自定义组件
    form-create-designer中怎么扩展自定义组件form-create-designer是基于 @form-create/element-ui实现的表单设计器组件。可以通过拖拽的方式快速创建表单,提高开发者对表单的开发效率,节省开发者的时间。FormCreate官网:https://www.form-create.com帮助文档:https://pro.form-cr......
  • 高并发系统-使用自定义日志埋点快速排查问题
    背景在高并发的系统中,通常不会打印除参数校验失败或捕获异常之外的日志,防止对接口的性能产生影响。那对于请求不符合预期的情况,我们如何快速找到是哪块逻辑影响的至关重要。Pfinder提供的链路监控,更多的是性能层面的监控,无法满足我们上述的诉求。下面我将通过自定义通用上下......
  • spring boot如何自定义注解
    总共分三步:1、创建一个注解importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Target(ElementType.METHOD)//注解的目标为方法@Retention(Retention......
  • QT基础语法与控件
    1.基础使用纯正的开源版本QT使用C++来实现QT使用QT可以使C++项目可视化本身也是C++的一个库允许跨平台QT特征面向对象,模块化设计调用,所有QT控件可继承控件之间的通信,signalslot友好的联机帮助,函数参数手册自定义控件设计QTCreator编译器集成开发环境IDE直接下载QT......
  • 即构 UIKits 重磅发布!高效开发与自定义UI兼备,打造互动场景新标杆
    即构UIKits上线,新一代场景化实时互动SDK!即构科技发布了首款面向中小团队的整合型实时互动产品UIKits,基于场景化最佳实践,整合RTC、IM、直播、美颜等多款产品,打造了音视频通话UIKit(CallKit)、互动直播UIKit(LiveStreamingKit)、语聊房UIKit(LiveAudioRoomKit)等多个场景互动SDK......
  • Zabbix添加自定义监控项
    1.查看zabbix_agent配置文件cat/etc/zabbix/zabbix_agentd.conf|grep-v"^$"|grep-v"^#"上图中,自定义监控项的配置文件可以放到Include项目中,Include可以有多个2.添加或修改自定义监控项配置文件cd/etc/zabbix/zabbix_agentd.d/vicustomized_cpu.confUserParameter......
  • 【C#】WPF字典资源设置Button控件样式
    <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Stylex:Key="BtnInfoStyle"TargetType="Button">......
  • 如何正确实现一个自定义可序列化的 Exception
    最近在公司的项目中,编写了几个自定义的Exception类。提交PR的时候,sonarqube提示这几个自定义异常不符合ISerializablepatten.花了点时间稍微研究了一下,把这个问题解了。今天在此记录一下,可能大家都会帮助到大家。自定义异常#编写一个自定义的异常,继承自Exception,其中......