一:添加自定义分页控件,命名为KDataPagerTwo:

 public class KDataPagerTwo : Control, INotifyPropertyChanged
{
static KDataPagerTwo()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(KDataPagerTwo), new FrameworkPropertyMetadata(typeof(KDataPagerTwo)));
} public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
if (propertyName == "PagerIndex")
{
if (PagerIndex != )
ChosenNumber();
}
else if (propertyName == "PagerTotal" || propertyName == "PagerSize")
Refresh();
} #region 变量定义
public TextBox txt_Jump;
public TextBlock txt_One;
public TextBlock txt_Two;
public TextBlock txt_Three;
public TextBlock txt_Four;
public TextBlock txt_Five;
public TextBlock txt_Six;
public TextBlock txt_Total;
/// <summary>
/// 首页
/// </summary>
public Image Img_HomePage;
/// <summary>
/// 上一页
/// </summary>
public Image Img_PreviousPage;
/// <summary>
/// 下一页
/// </summary>
public Image Img_NextPage;
/// <summary>
/// 尾页
/// </summary>
public Image Img_TailPage;
/// <summary>
/// 跳转按钮
/// </summary>
public Button btn_Ok;
#endregion #region 依赖属性
/// <summary>
/// 页大小
/// </summary>
public int PagerSize
{
get { return (int)GetValue(PagerSizeProperty); }
set { SetValue(PagerSizeProperty, value); OnPropertyChanged("PagerSize"); }
} /// <summary>
/// 当前页
/// </summary>
public int PagerIndex
{
get { return (int)GetValue(PagerIndexProperty); }
set { SetValue(PagerIndexProperty, value); OnPropertyChanged("PagerIndex"); }
} /// <summary>
/// 总计录数
/// </summary>
public int PagerTotal
{
get { return (int)GetValue(PagerTotalProperty); }
set { SetValue(PagerTotalProperty, value); OnPropertyChanged("PagerTotal"); }
} /// <summary>
/// 总页数
/// </summary>
public int PagerCount
{
get { return (int)GetValue(PagerCountProperty); }
set { SetValue(PagerCountProperty, value); OnPropertyChanged("PagerCount"); }
} //使用一个依赖属性作为PagerCount的后备存储器。这支持动画、样式、绑定等。
public static readonly DependencyProperty PagerCountProperty =
DependencyProperty.Register("PagerCount", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata()); //使用一个可靠的属性作为总的后备存储器。这支持动画、样式、绑定等
public static readonly DependencyProperty PagerTotalProperty =
DependencyProperty.Register("PagerTotal", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata()); //使用一个依赖属性作为PagerIndex的后备存储器。这支持动画、样式、绑定等。
public static readonly DependencyProperty PagerIndexProperty =
DependencyProperty.Register("PagerIndex", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata()); //使用一个依赖属性作为PagerSize的后备存储器。这支持动画、样式、绑定等。
public static readonly DependencyProperty PagerSizeProperty =
DependencyProperty.Register("PagerSize", typeof(int), typeof(KDataPagerTwo), new UIPropertyMetadata());
#endregion 依赖属性_end public override void OnApplyTemplate()
{
base.OnApplyTemplate(); Img_HomePage = GetTemplateChild("Img_HomePage") as Image;
Img_PreviousPage = GetTemplateChild("Img_PreviousPage") as Image;
Img_NextPage = GetTemplateChild("Img_NextPage") as Image;
Img_TailPage = GetTemplateChild("Img_TailPage") as Image;
btn_Ok = GetTemplateChild("btn_Ok") as Button;
txt_Jump = GetTemplateChild("txt_Jump") as TextBox; txt_One = GetTemplateChild("txt_One") as TextBlock;
txt_Two = GetTemplateChild("txt_Two") as TextBlock;
txt_Three = GetTemplateChild("txt_Three") as TextBlock;
txt_Four = GetTemplateChild("txt_Four") as TextBlock;
txt_Five = GetTemplateChild("txt_Five") as TextBlock;
txt_Six = GetTemplateChild("txt_Six") as TextBlock;
txt_Total = GetTemplateChild("txt_Total") as TextBlock;
// 绑定事件
Img_HomePage.MouseLeftButtonUp += Img_HomePage_MouseLeftButtonUp;
Img_PreviousPage.MouseLeftButtonUp += Img_PreviousPage_MouseLeftButtonUp;
Img_NextPage.MouseLeftButtonUp += Img_NextPage_MouseLeftButtonUp;
Img_TailPage.MouseLeftButtonUp += Img_TailPage_MouseLeftButtonUp;
btn_Ok.Click += Btn_Ok_Click; ;
txt_Jump.TextChanged += Txt_Jump_TextChanged; txt_One.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Two.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Three.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Four.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Five.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
txt_Six.MouseLeftButtonUp += Txt_MouseLeftButtonUp;
//刷新页数
Refresh();
} /// <summary>
/// 点击TextBlock事件
/// </summary>
private void Txt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if ((sender as TextBlock).Text.ToString() != "…")
PagerIndex = int.Parse((sender as TextBlock).Text);
} /// <summary>
/// 只能输入数字
/// </summary>
private void Txt_Jump_TextChanged(object sender, TextChangedEventArgs e)
{
//屏蔽中文输入和非法字符粘贴输入
TextBox textBox = sender as TextBox;
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, );
int offset = change[].Offset;
if (change[].AddedLength > )
{
double num = ;
if (!Double.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[].AddedLength);
textBox.Select(offset, );
}
}
} /// <summary>
/// 跳转
/// </summary>
private void Btn_Ok_Click(object sender, RoutedEventArgs e)
{
int txt = int.Parse(txt_Jump.Text.ToString() == "" ? "" : txt_Jump.Text.ToString());
if (txt > && txt <= PagerCount)
PagerIndex = txt;
} /// <summary>
/// 首页
/// </summary>
private void Img_HomePage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
PagerIndex = ;
} /// <summary>
/// 尾页
/// </summary>
private void Img_TailPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
PagerIndex = PagerCount;
} /// <summary>
/// 上一页
/// </summary>
private void Img_PreviousPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (PagerIndex > )
PagerIndex--;
} /// <summary>
/// 下一页
/// </summary>
private void Img_NextPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (PagerIndex < PagerCount)
PagerIndex++;
} #region 方法 /// <summary>
/// 选中数字的样式
/// </summary>
public void ChosenNumber()
{
if (PagerIndex > (PagerCount - ))
{
ColorChanged( - (PagerCount - PagerIndex)); LatterNumberChanged(PagerCount);
}
else
{
ColorChanged();
ForeFiveNumberChanged(PagerIndex);
LatterTwo();
}
} SolidColorBrush ScbBlue = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#193A57"));//蓝色
SolidColorBrush ScbRed = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E92F2F"));//红色 /// <summary>
/// 当前页变为红色
/// </summary>
public void ColorChanged(int GOTO)
{
txt_One.Foreground = ScbBlue;
txt_Two.Foreground = ScbBlue;
txt_Three.Foreground = ScbBlue;
txt_Four.Foreground = ScbBlue;
txt_Five.Foreground = ScbBlue;
txt_Six.Foreground = ScbBlue;
switch (GOTO)
{
case :
goto GT1;
case :
goto GT2;
case :
goto GT3;
case :
goto GT4;
case :
goto GT5;
case :
goto GT6;
}
GT1: txt_One.Foreground = ScbRed;
return;
GT2: txt_Two.Foreground = ScbRed;
return;
GT3: txt_Three.Foreground = ScbRed;
return;
GT4: txt_Four.Foreground = ScbRed;
return;
GT5: txt_Five.Foreground = ScbRed;
return;
GT6: txt_Six.Foreground = ScbRed;
} /// <summary>
/// 前四个数字变化
/// </summary>
/// <param name="InitialNumber">开始数字</param>
public void ForeFiveNumberChanged(int InitialNumber)
{
txt_One.Text = InitialNumber.ToString();
txt_Two.Text = (InitialNumber + ).ToString();
txt_Three.Text = (InitialNumber + ).ToString();
txt_Four.Text = (InitialNumber + ).ToString();
} /// <summary>
/// 设置后两位数字
/// </summary>
public void LatterTwo()
{
txt_Six.Text = PagerCount.ToString();
if (PagerCount > )
txt_Five.Text = "…";
else
txt_Five.Text = (PagerCount - ).ToString();
} /// <summary>
/// 数字从尾数开始变化
/// </summary>
/// <param name="Mantissa">尾数</param>
public void LatterNumberChanged(int Mantissa)
{
txt_Six.Text = Mantissa.ToString();
txt_Five.Text = (Mantissa - ).ToString();
txt_Four.Text = (Mantissa - ).ToString();
txt_Three.Text = (Mantissa - ).ToString();
txt_Two.Text = (Mantissa - ).ToString();
txt_One.Text = (Mantissa - ).ToString();
} /// <summary>
/// 设置总页数
/// </summary>
public void SetPagerCount()
{
int pc = PagerTotal / PagerSize;
if (PagerTotal % PagerSize == )
PagerCount = pc;
else
PagerCount = pc + ;
if (PagerCount <= )
CollapsedTXT(PagerCount);
txt_Total.Text = PagerTotal.ToString();
} /// <summary>
/// 小于6页的隐藏部分控件
/// </summary>
/// <param name="CollapsedStartTXT">从第几个开始</param>
public void CollapsedTXT(int CollapsedStartTXT)
{
switch (CollapsedStartTXT)
{
case :
goto CST1;
case :
goto CST2;
case :
goto CST3;
case :
goto CST4;
case :
goto CST5;
}
return;
CST1: txt_Two.Visibility = Visibility.Collapsed;
CST2: txt_Three.Visibility = Visibility.Collapsed;
CST3: txt_Four.Visibility = Visibility.Collapsed;
CST4: txt_Five.Visibility = Visibility.Collapsed;
CST5: txt_Six.Visibility = Visibility.Collapsed;
} /// <summary>
/// 刷新
/// </summary>
public void Refresh()
{
SetPagerCount();
ForeFiveNumberChanged(PagerIndex);
ColorChanged(PagerIndex);
LatterTwo();
}
#endregion
}

二:定义资源字典文件,命名为DataPagerTwo:

说明:local:KImgButton,这个也是一个自定义控件,可以改成Button控件也没有问题

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BaseControl"
>
<Style TargetType="Image" x:Key="Img_Size" >
<Setter Property="Width" Value="14"/>
<Setter Property="Height" Value="14"/>
</Style>
<Style TargetType="TextBlock" x:Key="Txt_Root">
<Setter Property="FontFamily" Value="新宋体"></Setter>
<Setter Property="FontSize" Value="14"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="#193A57"></Setter>
</Style>
<Style TargetType="TextBlock" x:Key="Txt_Side" BasedOn="{StaticResource Txt_Root}">
<Setter Property="Foreground" Value="#193A57"></Setter>
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
</Style>
<Style TargetType="TextBlock" x:Key="Txt_Margin" BasedOn="{StaticResource Txt_Root}">
<Setter Property="Margin" Value="4"/>
<Setter Property="Cursor" Value="Hand"></Setter>
</Style>
<Style TargetType="local:KImgButton">
<Setter Property="IsEnabled" Value="True"></Setter>
<Setter Property="CornerRadius" Value="2"></Setter>
<Setter Property="FIconSize" Value="0"></Setter>
</Style>
<Style TargetType="{x:Type local:KDataPagerTwo}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:KDataPagerTwo}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="10,0">
<TextBlock Style="{StaticResource Txt_Side}" Text="共"></TextBlock>
<TextBlock Style="{StaticResource Txt_Side}" Text="0" Margin="5,0" x:Name="txt_Total"></TextBlock>
<TextBlock Style="{StaticResource Txt_Side}" Text="条数据。"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="1" x:Name="Stack_Control" HorizontalAlignment="Right" Margin="10,0">
<Image x:Name="Img_HomePage" Source="/BaseControl;component/Images/DataPagerImages/First.png" Margin="5,0" Style="{ StaticResource Img_Size}" Cursor="Hand"/>
<Image x:Name="Img_PreviousPage" Source="/BaseControl;component/Images/DataPagerImages/prev.png" Style="{ StaticResource Img_Size}" Cursor="Hand"/> <TextBlock x:Name="txt_One" Text="1" Style="{StaticResource Txt_Margin}"/>
<TextBlock x:Name="txt_Two" Text="2" Style="{StaticResource Txt_Margin}"/>
<TextBlock x:Name="txt_Three" Text="3" Style="{StaticResource Txt_Margin}"/>
<TextBlock x:Name="txt_Four" Text="4" Style="{StaticResource Txt_Margin}"/>
<TextBlock x:Name="txt_Five" Text="●●●" Style="{StaticResource Txt_Margin}"/>
<TextBlock x:Name="txt_Six" Text="10" Style="{StaticResource Txt_Margin}"/> <Image x:Name="Img_NextPage" Source="/BaseControl;component/Images/DataPagerImages/Next.png" Style="{ StaticResource Img_Size}" Cursor="Hand"/>
<Image x:Name="Img_TailPage" Source="/BaseControl;component/Images/DataPagerImages/Last.png" Margin="5,0,20,0" Style="{ StaticResource Img_Size}" Cursor="Hand"/> <TextBlock Text="到第" Style="{StaticResource Txt_Root}" Margin="-5,5,5,5"></TextBlock> <Border Margin="0,5,5,5" Background="#4081D1" BorderBrush="#4081D1" Width="19" Height="19">
<TextBox x:Name="txt_Jump" FontFamily="微软雅黑" VerticalContentAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" FontSize="12" Width="17" Height="17" BorderThickness="0"/>
</Border> <TextBlock Text="页" Style="{StaticResource Txt_Root}" Margin="0,0,20,0"></TextBlock> <local:KImgButton x:Name="btn_Ok" FontFamily="新宋体" FontSize="14" Content="跳转" Height="20" Width="50" Background="#4081D1" Margin="-10,0,0,0"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

三:在Generic中引用资源字典文件:

    <ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BaseControl;component/Themes/DataPagerTwo.xaml" />
</ResourceDictionary.MergedDictionaries>

四:将Generic中下面代码删除:

<Style TargetType="{x:Type local:KDataPagerTwo}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:KDataPagerTwo}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

五:现在就可以用了,用法:

先引用:

再添加:

六:后台PropertyChanged事件代码(当前页数发生变化,就可以根据DataPager.PagerIndex传入分页方法):

        /// <summary>
/// 当某一属性值发生改变事件
/// </summary>
private void DataPager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//分页方法(根据需要可以写成其他分页方法)
       //DataPager.PagerIndex--当前页,DataPager.PagerSize--每页记录数
datagrid1.ItemsSource = TL.ToList().Skip((DataPager.PagerIndex - ) * DataPager.PagerSize).Take(DataPager.PagerSize).ToList();
}

WPF 自定义分页控件二的更多相关文章

  1. WPF自定义分页控件,样式自定义,简单易用

    WPF自定义分页控件 做了许久伸手党,终于有机会贡献一波,搜索一下WPF分页控件,还是多,但是不太通用,主要就是样式问题,这个WPF很好解决,还有一个就是分页控件嘛,只关心几个数字的变动就行了,把页码 ...

  2. WPF 自定义分页控件一

    一:右键添加新建项,选择新建自定义控件,命名为:KDataPager public class KDataPager : Control { static KDataPager() { Default ...

  3. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  4. asp.net webform 自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: & ...

  5. Mvc自定义分页控件

    MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...

  6. WPF管理系统自定义分页控件 - WPF特工队内部资料

    最近做一个演示的管理系统项目,需要用到分页控件,在网上找了很多,依然找到与UI模版匹配的,最后干脆自己写一个. 分页控件分析: 1.分页控件分简单显示和复杂显示两种: 2.包含上一页.下一页以及页码明 ...

  7. jquery 分页控件(二)

    上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的,我弄了个简单的asp.net ...

  8. 浅尝辄止WPF自定义用户控件(实现颜色调制器)

    主要利用用户控件实现一个自定义的颜色调制控件,实现一个小小的功能,具体实现界面如下. 首先自己新建一个wpf的用户控件类,我就放在我的wpf项目的一个文件夹下面,因为是一个很小的东西,所以就没有用mv ...

  9. Winform自定义分页控件的实现

    实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...

随机推荐

  1. Linux下使用Quagga(Zebra)搭建路由器记录(转)

    写在前面 从22号中午开始琢磨zebra/quagga的用法,一直到晚上11点多都没有什么头绪.各种Google,百度,几近崩溃.由于网上关于zebra/quagga的配置方法都是在真实的若干台电脑上 ...

  2. LOJ 2585 「APIO2018」新家 ——线段树分治+二分答案

    题目:https://loj.ac/problem/2585 算答案的时候要二分! 这样的话,就是对于询问位置 x ,二分出一个最小的 mid 使得 [ x-mid , x+mid ] 里包含所有种类 ...

  3. 分页-jquery.page.js插件在使用时重复触发“上一页”和“下一页”操作

    HTML代码 <!-- <div class="zxf_pagediv" id="Pagination" style="display:b ...

  4. Jmeter--报错 WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

    今天要用Jmeter测试服务器性能,发现GUI界面总是有warning提示: WARNING: Could not open/create prefs root node Software\JavaS ...

  5. jstack实战死循环与死锁学习笔记

    一.实战死循环导致CPU飙高 top -p pid -H jstack pid printf "%s"  十进制的线程id 二.创建CUP100%实例(死循环) 1.创建CpuCo ...

  6. Git-打标签

    打标签同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签.人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做.本节我们一起来学习如何列出所有可用的标签,如何新建标签,以及 ...

  7. 【java】函数概述

    函数也叫方法,是具有一定功能的小程序. 函数格式: 修饰符 返回值类型 函数名(参数类型 形式参数:参数类型 形式参数) { 执行语句: return 返回值; } 返回值类型:函数运行后结果的数据类 ...

  8. GPIO实验

    一.目标:点亮led 1.看原理图:怎样点亮led 2.怎样GPF4输出0/1 a.配置功能  输出/输入/其他功能(中断或者其他) b.设置输出高电平/低电平 操作寄存器--->看芯片手册 A ...

  9. Cygwin使用2-心得

    引用:http://www.jb51.net/article/6236.htm 1.在cygwin里访问Windows盘 cd /cygdrive/c cd c: 2.整合cygwin命令到Windo ...

  10. Hiero_FnNukeShotExporter的解析与修改

    研究对象:Hiero中的FnNukeShotExporter脚本 研究目的:修改FnNukeShotExporter使得可以将多个TrackItem导入到一个.nk中   FnNukeShotExpo ...