前序
做一个设备数据点调试工具。
需求
实现一个分页的组件,总数据、总分页、目标页、上一页、下一页和分页大小。
基础样式ui Handyui
文档地址
实现结果
思路
- 使用用户控件
- 使用自定义控件
使用用户控件已经满足实现分页控件,暂时不研究自定义控件。
自定义控件封装主要涉及
- 依赖属性
- 查询事件
控件内注册依赖属性并使用可以绑定更新
/// <summary>
/// 总条数
/// </summary>
public long Total
{
get { return (long)GetValue(TotalProperty); }
set { SetValue(TotalProperty, value); }
}
public static readonly DependencyProperty TotalProperty =
DependencyProperty.Register("Total", typeof(long), typeof(Pagination), new PropertyMetadata(0l, (s, e) =>
{
((Pagination)s).Total = (long)e.NewValue;
}));
控件内注册依赖属性命令类型
/// <summary>
/// 上一页
/// </summary>
public static readonly DependencyProperty FindPreCommandProperty =
DependencyProperty.Register("PreCommand", typeof(RelayCommand), typeof(Pagination), new PropertyMetadata(null,
(o, args) =>
{
((Pagination)o).PreCommand = (RelayCommand)args.NewValue;
} ));
public RelayCommand PreCommand
{
get { return (RelayCommand)GetValue(FindPreCommandProperty); }
set { SetValue(FindPreCommandProperty, value); }
}
编码
控件 Pagination.xaml
<UserControl x:Class="DeviceData.Views.Controls.Pagination"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DeviceData.Views.Controls"
xmlns:hc="https://handyorg.github.io/handycontrol"
mc:Ignorable="d"
Background="Transparent" Height="65" Width="680">
<StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Center">
<TextBlock Text="共计:" VerticalAlignment="Center" Foreground="White" Background="Transparent" />
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Total}" Margin="20,0,0,0"
Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<TextBlock Text="条数据" Margin="20,0,0,0" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<TextBlock Text="共" Margin="20,0,0,0" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Page, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10,0,0,0"
Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<TextBlock Text="页" Margin="10,0,0,0" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Current, Mode=OneWay}"
Margin="20,0,0,0" Width="60" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=GoCommand}"
Content="GO" Margin="20,0,0,0" Width="40" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=PreCommand}"
Content="上一页" Margin="20,0,0,0" Width="80" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=NextCommand}"
Content="下一页" Margin="20,0,0,0" Width="80" Foreground="White" Background="Transparent" VerticalAlignment="Center"/>
<ComboBox hc:DropDownElement.ConsistentWidth="False" Width="80" Foreground="Green" Background="Transparent" Margin="30,0,0,0"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PageSizes}"
SelectedValue="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=PageSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0"
/>
</StackPanel>
</UserControl>
控件 Pagination.cs
using System.Windows;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.Input;
namespace DeviceData.Views.Controls;
public partial class Pagination : UserControl
{
public Pagination()
{
InitializeComponent();
}
#region 分页数据属性
/// <summary>
/// 总条数
/// </summary>
public long Total
{
get { return (long)GetValue(TotalProperty); }
set { SetValue(TotalProperty, value); }
}
public static readonly DependencyProperty TotalProperty =
DependencyProperty.Register("Total", typeof(long), typeof(Pagination), new PropertyMetadata(0l, (s, e) =>
{
((Pagination)s).Total = (long)e.NewValue;
}));
/// <summary>
/// 总页数
/// </summary>
public int Page
{
get { return (int)GetValue(PageProperty); }
set { SetValue(PageProperty, value); }
}
public static readonly DependencyProperty PageProperty =
DependencyProperty.Register("Page", typeof(int), typeof(Pagination),
new PropertyMetadata(0, (o, args) =>
{
((Pagination)o).Page = (int)args.NewValue;
}));
/// <summary>
/// 当前页
/// </summary>
public int Current
{
get { return (int)GetValue(CurrentProperty); }
set { SetValue(CurrentProperty, value); }
}
public static readonly DependencyProperty CurrentProperty =
DependencyProperty.Register("Current", typeof(int), typeof(Pagination), new PropertyMetadata(1, (o, args) =>
{
((Pagination)o).Page = (int)args.NewValue;
}));
/// <summary>
/// 分页大小列表
/// </summary>
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(Pagination), new PropertyMetadata(null, (o, args) =>
{
((Pagination)o).PageSizes = (List<int>)args.NewValue;
}));
/// <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(Pagination), new PropertyMetadata(0, (o, args) =>
{
((Pagination)o).PageSize = (int)args.NewValue;
}));
#endregion
#region 分页事件
/// <summary>
/// 上一页
/// </summary>
public static readonly DependencyProperty FindPreCommandProperty =
DependencyProperty.Register("PreCommand", typeof(RelayCommand), typeof(Pagination), new PropertyMetadata(null,
(o, args) =>
{
((Pagination)o).PreCommand = (RelayCommand)args.NewValue;
} ));
public RelayCommand PreCommand
{
get { return (RelayCommand)GetValue(FindPreCommandProperty); }
set { SetValue(FindPreCommandProperty, value); }
}
public static readonly DependencyProperty NextCommandProperty =
DependencyProperty.Register("NextCommand", typeof(RelayCommand), typeof(Pagination), new PropertyMetadata(null,
(o, args) =>
{
((Pagination)o).NextCommand = (RelayCommand)args.NewValue;
} ));
public RelayCommand NextCommand
{
get { return (RelayCommand)GetValue(NextCommandProperty); }
set { SetValue(NextCommandProperty, value); }
}
public static readonly DependencyProperty GoCommandProperty =
DependencyProperty.Register("GoCommand", typeof(RelayCommand), typeof(Pagination), new PropertyMetadata(null,
(o, args) =>
{
((Pagination)o).GoCommand = (RelayCommand)args.NewValue;
} ));
public RelayCommand GoCommand
{
get { return (RelayCommand)GetValue(GoCommandProperty); }
set { SetValue(GoCommandProperty, value); }
}
#endregion
}
使用控件窗体或控件
- 导入命名控件
xmlns:ctls="clr-namespace:DeviceData.Views.Controls"
- 使用控件,通过ViewModel进行数据绑定
<ctls:Pagination Margin="5" Total="{Binding PagenationModel.Total}" Current="{Binding PagenationModel.Current}"
Page="{Binding PagenationModel.page}" PageSize="{Binding PagenationModel.Pages}"
PageSizes="{Binding PagenationModel.PageSizes}" PreCommand="{Binding PagenationModel.FindPreCommand}"
NextCommand="{Binding PagenationModel.FindNextCommand}" GoCommand="{Binding PagenationModel.FindTargetCommand}"
>
</ctls:Pagination>
封装的分页数据模型 (参考)
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace DeviceData.Models;
public class PagenationModel<T> : ObservableObject
{
private long total = 0;
public long Total
{
get => total;
set => SetProperty(ref total, value);
}
private int page = 1;
public int Page
{
get => page;
set => SetProperty(ref page, value);
}
private int current = 1;
public int Current
{
get => current;
set => SetProperty(ref current, value);
}
private List<int> pageSizes = new List<int>(){10, 20, 30, 40, 50};
public List<int> PageSizes
{
get => pageSizes;
set => SetProperty(ref pageSizes, value);
}
/// <summary>
/// 上一页
/// </summary>
private RelayCommand findPreCommand;
public RelayCommand FindPreCommand
{
get => findPreCommand;
set => SetProperty(ref findPreCommand, value);
}
/// <summary>
/// 下一页
/// </summary>
private RelayCommand findNextCommand;
public RelayCommand FindNextCommand
{
get => findNextCommand;
set => SetProperty(ref findNextCommand, value);
}
/// <summary>
/// 目标页
/// </summary>
private RelayCommand findTargetCommand;
public RelayCommand FindTargetCommand
{
get => findTargetCommand;
set => SetProperty(ref findTargetCommand, value);
}
private ObservableCollection<T> rows = new ObservableCollection<T>();
public ObservableCollection<T> Rows
{
get => rows;
set => SetProperty(ref rows, value);
}
}
MVVM框架用的是微软的 CommunityToolkit.Mvvm
官方文档
效果图
软件暂未开发完成,等开发完成再把源码地址放出来。