一:添加自定义分页控件,命名为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. USB引脚定义

  2. php限制文件下载速度的代码

    有时候你会出于某种目的而要求把下载文件的速度放慢一些,例如你想实现文件下载进度条功能.限制下载速度最大的好处是节省带宽,避免瞬时流量过大而造成网络堵塞.本文将和你分享如何通过php代码来实现限制文件的 ...

  3. msp430学习笔记-msp430g2553

    C语言例程:http://wenku.baidu.com/link?url=49JzNSvt3m0fRuf8SWTEM8yEw1yzqr4lBR-QbX8FddcmjTVYnDhuR97wB60HNf ...

  4. Git的一些东西(后续补充)

    查看帮助,要装git-doc,另外推荐git的图形客户端gitg,比gitk好看多了,用apt-get install就可   HEAD是当前工作版本的指针   --global保存的是当前用户的配置 ...

  5. WCF类型共享技巧【转载】

    调用过WCF服务的同学可能都会遇到这样的问题,同一个实体类型,不同的服务Visual Studio生成了不同的版本,例如Service1.User和Service2.User,对于C#来说,这是两个不 ...

  6. Windows右键菜单中新建项目添加与删除

    一种是如  txt 类型: HKEY_CLASSES_ROOT\.txt\ShellNew 项下空字符串值:NullFile 另一种如MsOffice类型: HKEY_CLASSES_ROOT\.xl ...

  7. 【mysql】ICP下mysql执行计划的一次解析

    mysql版本 [root@xxxx]# mysql --version mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using ...

  8. this、apply/call、bind、闭包、函数、变量复制

    一.实际场景中抽象出的一个问题 下面this各指向什么? var a = { b: function() { console.log(this); }, f: function() { var c = ...

  9. 外观(Facade)模式

    外观模式:为子系统中的一组接口提供一个一致的界面.此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用 在软件开发中,有时候为了完成一项较为复杂的功能,一个客户类需要和多个业务类交互,而这些需 ...

  10. computer、methods和watch

    在vue中处理复杂的逻辑的时候,我们经常使用计算属性computer,但是很多时候,我们会把计算属性.方法和侦听器搞混淆,在 w3cplus.com的一篇文章中是这样总结这三者的. methods:正 ...