WPF 自定义分页控件二
一:添加自定义分页控件,命名为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 自定义分页控件二的更多相关文章
- WPF自定义分页控件,样式自定义,简单易用
WPF自定义分页控件 做了许久伸手党,终于有机会贡献一波,搜索一下WPF分页控件,还是多,但是不太通用,主要就是样式问题,这个WPF很好解决,还有一个就是分页控件嘛,只关心几个数字的变动就行了,把页码 ...
- WPF 自定义分页控件一
一:右键添加新建项,选择新建自定义控件,命名为:KDataPager public class KDataPager : Control { static KDataPager() { Default ...
- C# DataGridView自定义分页控件
好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...
- asp.net webform 自定义分页控件
做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: & ...
- Mvc自定义分页控件
MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...
- WPF管理系统自定义分页控件 - WPF特工队内部资料
最近做一个演示的管理系统项目,需要用到分页控件,在网上找了很多,依然找到与UI模版匹配的,最后干脆自己写一个. 分页控件分析: 1.分页控件分简单显示和复杂显示两种: 2.包含上一页.下一页以及页码明 ...
- jquery 分页控件(二)
上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的,我弄了个简单的asp.net ...
- 浅尝辄止WPF自定义用户控件(实现颜色调制器)
主要利用用户控件实现一个自定义的颜色调制控件,实现一个小小的功能,具体实现界面如下. 首先自己新建一个wpf的用户控件类,我就放在我的wpf项目的一个文件夹下面,因为是一个很小的东西,所以就没有用mv ...
- Winform自定义分页控件的实现
实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...
随机推荐
- composer 安装yii2 The package is not available in a stable-enough version解决办法
错误 Potential causes: - A typo in the package name - The package is not available in a stable-enough ...
- Rectified/无限流量/KVM/1G内存/亚洲优化/月付3.99刀起/商家首次续费优惠/91yun第600篇博文
具体配置如下: 1v CPU + 1G 内存 + 10G SSD + 无限流量( 30T ) + 100M 口 + FreeBSD 支持 + 1IPv4 + IPv6 (可工单) + rDNS = 3 ...
- OpenWrt路由器通过LuCI界面实现Guest SSID功能
转自: http://blog.ltns.info/linux/guest_ssid_over_openwrt_router/ 之前尝试过 Tomato路由器设置VLAN实现Guest SSID功能, ...
- SDI工程时钟路径分析
SDI工程时钟路径分析 //------------- Receive Ports - RX Fabric Output Control Ports ------------- output rxou ...
- C#编程的最佳工具
C#是企业中广泛使用的编程语言,特别是那些依赖微软的程序语言.如果您使用C#构建应用程序,则最有可能使用Visual Studio,并且已经寻找了一些扩展来对您的开发进行管理.但是,这个工具列表可能会 ...
- linux 简单笔记
Linux查看端口使用状态.关闭端口方法 http://blog.csdn.net/wudiyi815/article/details/7473097
- 解决js输出汉字乱码问题
当我们需要使用js输出汉字时,偶然会出现输出的中文汉字乱码的情况,在网上收了很多解决方案 1.在mata中加 <meta content="text/html; charset=utf ...
- 【java】之正则表达式摘要
构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn (0 <= n < ...
- 使用Google cardboard 2的一些软件
最近入手cardboard2,FQ尝试了一些软件,特别分享,给大家提供一些方便. 链接:http://pan.baidu.com/s/1slehilZ 密码:b49h
- 阿里云ECS安装Kubernetes问题收集与解答
问题1 kubernetes pod启动报错open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such fil ...