WPF自定义DataGrid分页控件
新建Custom Control,名:PagingDataGrid
打开工程下面的Themes\Generic.xaml
xaml里面代码替换如下
<Style x:Key="{x:Type loc:PagingDataGrid}" TargetType="{x:Type loc:PagingDataGrid}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="#FF688CAF" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="CanUserSortColumns" Value="False"/>
<Setter Property="AlternatingRowBackground" Value="SkyBlue"/>
<Setter Property="AlternationCount" Value="2"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type loc:PagingDataGrid}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" Padding="{TemplateBinding Padding}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0" Focusable="false" Name="DG_ScrollViewer">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Command="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}">
<Button.Visibility>
<Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
<Binding.ConverterParameter>
<DataGridHeadersVisibility>All</DataGridHeadersVisibility>
</Binding.ConverterParameter>
</Binding>
</Button.Visibility>
</Button>
<DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
<DataGridColumnHeadersPresenter.Visibility>
<Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
<Binding.ConverterParameter>
<DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
</Binding.ConverterParameter>
</Binding>
</DataGridColumnHeadersPresenter.Visibility>
</DataGridColumnHeadersPresenter>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1"/>
<ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
<!--分页控件-->
<StackPanel Grid.Row="1" SnapsToDevicePixels="True" VerticalAlignment="Center" Visibility="{Binding IsShowPaging,RelativeSource={RelativeSource TemplatedParent},Converter={StaticResource BoolToVisibilityConverter}}" Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="13"/>
<Setter Property="Margin" Value="3,0"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="13"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="FontSize" Value="13"/>
</Style>
<Style TargetType="Button">
<Setter Property="FontSize" Value="13"/>
</Style>
</StackPanel.Resources>
<TextBlock>总计</TextBlock>
<TextBlock Foreground="Red" Text="{Binding Total,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" />
<TextBlock>条记录 每页显示</TextBlock>
<ComboBox x:Name="PART_DispalyCount" BorderThickness="1" BorderBrush="Red" Foreground="Red" Grid.Column="0" FontSize="12" VerticalAlignment="Center" MinWidth="40" ItemsSource="{Binding PageSizeItemsSource,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}" SelectedItem="{Binding PageSize,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock>条 总页数</TextBlock>
<TextBlock Margin="3" Foreground="Red" Text="{Binding PageCount,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" />
<Button x:Name="PART_PrePage" Content="上一页"/>
<TextBlock>当前页</TextBlock>
<TextBox Width="100" Foreground="Red" x:Name="PART_PageIndex" Text="{Binding PageIndex,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" />
<TextBlock>页</TextBlock>
<Button x:Name="PART_GoTo" Content="转到页"/>
<Button x:Name="PART_NextPage" Content="下一页"/>
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</Style.Triggers>
</Style>
打开PagingDataGrid.cs文件
代码如下:
public class PagingDataGrid : DataGrid
{
static PagingDataGrid()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(PagingDataGrid), new FrameworkPropertyMetadata(typeof(PagingDataGrid)));
} #region 变量定义
ComboBox PART_DispalyCount;
Button PART_PrePage;
Button PART_NextPage;
Button PART_GoTo;
TextBox PART_PageIndex;
#endregion #region 事件代理 public delegate void PagingChangedEventHandler(object sender, PagingChangedEventArgs e); public event PagingChangedEventHandler PagingChanged; private void OnPagingChanged()
{
if (PagingChanged != null)
{
PagingChanged(this, new PagingChangedEventArgs() { PageIndex = PageIndex, PageSize = PageSize });
}
}
#endregion #region 一般属性
private List<Student> dataItems = new List<Student>(); public List<Student> DataItems
{
get
{
return dataItems;
}
set
{
dataItems = value;
ReCalcLayout();
}
} List<int> list = new List<int>(); public List<int> PageSizeItemsSource
{
get
{
return list;
}
set
{
list = value;
}
}
#endregion #region 依赖属性
/// <summary>
/// 页大小
/// </summary>
public int PageSize
{
get { return (int)GetValue(PageSizeProperty); }
set { SetValue(PageSizeProperty, value); }
} /// <summary>
/// 当前页
/// </summary>
public int PageIndex
{
get { return (int)GetValue(PageIndexProperty); }
set { SetValue(PageIndexProperty, value); }
} /// <summary>
/// 总计录数
/// </summary>
public int Total
{
get { return (int)GetValue(TotalProperty); }
set { SetValue(TotalProperty, value); }
} /// <summary>
/// 是否显示页
/// </summary>
public bool IsShowPaging
{
get { return (bool)GetValue(IsShowPagingProperty); }
set { SetValue(IsShowPagingProperty, value); }
} /// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get { return (int)GetValue(PageCountProperty); }
set { SetValue(PageCountProperty, value); }
} // Using a DependencyProperty as the backing store for PageCount. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageCountProperty =
DependencyProperty.Register("PageCount", typeof(int), typeof(PagingDataGrid), new UIPropertyMetadata()); // Using a DependencyProperty as the backing store for End. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EndProperty =
DependencyProperty.Register("End", typeof(int), typeof(PagingDataGrid), new UIPropertyMetadata()); // Using a DependencyProperty as the backing store for Start. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StartProperty =
DependencyProperty.Register("Start", typeof(int), typeof(PagingDataGrid), new UIPropertyMetadata()); // Using a DependencyProperty as the backing store for IsShowPaging. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsShowPagingProperty =
DependencyProperty.Register("IsShowPaging", typeof(bool), typeof(PagingDataGrid), new UIPropertyMetadata(true)); // Using a DependencyProperty as the backing store for Total. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TotalProperty =
DependencyProperty.Register("Total", typeof(int), typeof(PagingDataGrid), new UIPropertyMetadata()); // Using a DependencyProperty as the backing store for PageIndex. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageIndexProperty =
DependencyProperty.Register("PageIndex", typeof(int), typeof(PagingDataGrid), new UIPropertyMetadata()); // Using a DependencyProperty as the backing store for PageSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageSizeProperty =
DependencyProperty.Register("PageSize", typeof(int), typeof(PagingDataGrid), new UIPropertyMetadata()); #endregion #region 相关命令
RoutedCommand NextPageCommand = new RoutedCommand();
RoutedCommand PrePageCommand = new RoutedCommand();
RoutedCommand GoToCommand = new RoutedCommand();
#endregion #region 重写方法
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//获取模板中的控件
PART_DispalyCount = GetTemplateChild("PART_DispalyCount") as ComboBox;
PART_PrePage = GetTemplateChild("PART_PrePage") as Button;
PART_NextPage = GetTemplateChild("PART_NextPage") as Button;
PART_GoTo = GetTemplateChild("PART_GoTo") as Button;
PART_PageIndex = GetTemplateChild("PART_PageIndex") as TextBox;
//添加控件命令Binding
PART_NextPage.CommandBindings.Add(new CommandBinding(NextPageCommand, NextPage_Excuted, NextPage_CanExcuted));
PART_PrePage.CommandBindings.Add(new CommandBinding(PrePageCommand, PrePage_Excuted, PrePage_CanExcuted));
PART_GoTo.CommandBindings.Add(new CommandBinding(GoToCommand, GoTo_Excuted, GoTo_CanExcuted));
PART_NextPage.Command = NextPageCommand;
PART_PrePage.Command = PrePageCommand;
PART_GoTo.Command = GoToCommand;
//添加控件事件
PART_DispalyCount.SelectionChanged += PART_DispalyCount_SelectionChanged;
} private void GoTo_Excuted(object sender, ExecutedRoutedEventArgs e)
{
int idx = ;
if (int.TryParse(PART_PageIndex.Text, out idx))
{
GoTo(idx);
}
} private void GoTo_CanExcuted(object sender, CanExecuteRoutedEventArgs e)
{
int idx = ;
if (int.TryParse(PART_PageIndex.Text, out idx))
{
if (idx > PageCount || idx <= )
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
}
else
{
e.CanExecute = false;
}
} private void NextPage_CanExcuted(object sender, CanExecuteRoutedEventArgs e)
{
if (PageIndex == PageCount)
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
} private void NextPage_Excuted(object sender, ExecutedRoutedEventArgs e)
{
GoTo(PageIndex++);
} private void PrePage_CanExcuted(object sender, CanExecuteRoutedEventArgs e)
{
if (PageIndex == )
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
} private void PrePage_Excuted(object sender, ExecutedRoutedEventArgs e)
{
GoTo(PageIndex--);
}
#endregion #region 分页事件
void PART_DispalyCount_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PageSize = int.Parse((sender as ComboBox).SelectedItem.ToString());
ReCalcLayout();
GoTo(PageIndex);
} private void GoTo(int page)
{
ItemsSource = DataItems.Skip((PageIndex - ) * PageSize).Take(PageSize).ToList();
} private void ReCalcLayout()
{
Total = DataItems.Count;
int pc = Total / PageSize;
if (Total % PageSize == )
{
PageCount = pc;
}
else
{
PageCount = pc + ;
}
PageIndex = PageIndex > PageCount ? PageCount : PageIndex;
}
#endregion
}
此外还需要一个类支持PagingChangedEventArgs
/// <summary>
/// 页跳转参数
/// </summary>
public class PagingChangedEventArgs : EventArgs
{
public int PageSize { get; set; }
public int PageIndex { get; set; }
}
控件完成,调试:
<local:PagingDataGrid x:Name="pdg" PagingChanged="pdg_PagingChanged" IsShowPaging="True" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" SnapsToDevicePixels="True" CanUserReorderColumns="False">
<local:PagingDataGrid.PageSizeItemsSource>
<sys:Int32>5</sys:Int32>
<sys:Int32>10</sys:Int32>
<sys:Int32>15</sys:Int32>
<sys:Int32>20</sys:Int32>
<sys:Int32>30</sys:Int32>
<sys:Int32>50</sys:Int32>
<sys:Int32>100</sys:Int32>
</local:PagingDataGrid.PageSizeItemsSource>
<local:PagingDataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="AAA"/>
<DataGridTextColumn Binding="{Binding Age}" Header="BBB"/>
<DataGridTextColumn Binding="{Binding Sex}" Width="*" Header="CCC"/>
</local:PagingDataGrid.Columns>
</local:PagingDataGrid>
新建调试类Student
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string MoreInfo { get; set; }
}
调试代码,注意这里使用DataItems保存信息,如果使用Items或是ItemsSource则无分页效果
List<Student> list = new List<Student>();
for (int i = ; i < ; i++)
{
list.Add(new Student() { Name = Guid.NewGuid().ToString(), Age = i, MoreInfo = "Sex" });
}
pdg.DataItems = list;
WPF自定义DataGrid分页控件的更多相关文章
- WPF里DataGrid分页控件
1.主要代码: using System; using System.Collections.ObjectModel; using System.Windows; using System.Windo ...
- WPF自定义选择年月控件详解
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 自定义angularjs分页控件
继昨天写了knockoutjs+ jquery pagination+asp.net web Api 实现无刷新列表页 ,正好最近刚学习angularjs ,故琢磨着写一个angularjs版本的分页 ...
- [转]Oracle分页之二:自定义web分页控件的封装
本文转自:http://www.cnblogs.com/scy251147/archive/2011/04/16/2018326.html 上节中,讲述的就是Oracle存储过程分页的使用方式,但是如 ...
- C# WinForm自定义通用分页控件
大家好,前几天因工作需要要开发一个基于WinForm的小程序.其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都 ...
- WPF简单的分页控件实现
XAML代码(使用ItemsControl控件实现): <UserControl x:Class="SunCreate.Vipf.Client.UI.CityDoor.PageCont ...
- WPF 自定义DateControl DateTime控件
自定义日期控件,月份选择.如下是日期的一些效果图. 具体的样式.颜色可以根据下面的代码,自己调节即可 1.日期控件的界面 <UserControl x:Class="WpfApp ...
- WPF 自定义DateControl DateTime控件(转)
自定义日期控件,月份选择.如下是日期的一些效果图. 具体的样式.颜色可以根据下面的代码,自己调节即可 1.日期控件的界面 <UserControl x:Class="WpfApp ...
- WPF自定义数字输入框控件
要求:只能输入数字和小数点,可以设置最大值,最小值,小数点前长度,小数点后长度(支持绑定设置): 代码如下: using System; using System.Collections.Generi ...
随机推荐
- UVa 3704 Cellular Automaton(矩乘)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...
- NGRC远程控制
这是大三学完计算机网络的课程之后开始写的东西,后来慢慢完善到能用的程度. 界面大概是这样 还有这样 使用Java的好处是不用修改就可以在Mac和Linux上运行(测试过Ubuntu和OS X) 缺点是 ...
- SQL-LINQ-Lambda语法对照
SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Employees .Select ...
- Hibernate自定义数据库查询(排序、输出条数)
Hibernate数据库操作类(eg:TexDAO.java) /* * queryString HQL语句,first开始条数, max输出条数 ,norder排序 * 例: List lis = ...
- tomcat 配置https (单向认证)
1.单向认证,就是传输的数据加密过了,但是不会校验客户端的来源 2.双向认证,如果客户端浏览器没有导入客户端证书,是访问不了web系统的,找不到地址 如果只是加密,单向就行 如果想要用系统的人没有证书 ...
- java实现url转码、解码
URL由来: 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址 “http://www.abc.com”,但是没有希腊字母的网址“htt ...
- opencv学习笔记-图像叠加、混合
在图像处理中,目标区域定义为感兴趣区域ROI(region of Interest),这是后期图像处理的基础,在获取ROI后,进行一些列的处理.ROI区域在Opencv中就是Rect,先构建Rect, ...
- IOS 表视图UITableView 束NSBundle
今天搞了一下表视图UITableView 表视图是在以后应用程序开发中经常用到的一个视图,所以必须要熟练掌握 所获不多,对视图有了一个大概的了解 其中有用到NSBundle , 束 这个类 先说一 ...
- 各种Markdown处理器的列表
从MarkdownImplementations - Markdown Community Group可以找到一个实现markdown处理器的列表,如下: Name Language Type Des ...
- CSS3 经典教程系列:CSS3 线性渐变(linear-gradient)
摘自: http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-linear-gradient.html