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 ...
随机推荐
- App被拒绝的原因收录
转自:dApps开发者 » APP被苹果App Store拒绝的79个原因(未完待续) 1.程序有重大bug,程序不能启动,或者中途退出.2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币.3. ...
- Transaction: atomicity, consistency, separability, persistence
Transaction: atomicity, consistency, separability, persistence http://langgufu.iteye.com/blog/143440 ...
- Relocation - POJ 2923(状态压缩+01背包)
题目大意:有个人需要搬家,有N件物品,给个物品的重量是 w[i] 然后又两个车,每个车的载重量分别是C1和C2,求最少需要运输多少次才能把这些物品全部运输完毕. 分析:刚开始就发现物品数不多,想着直接 ...
- Web移动端Fixed布局的解决方案
移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况. 但是 fixed 元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题. 这篇文章里就提供一个简单的有 ...
- 微软提供的API的各个版本之间的区别
First Floor Software这个diff lists非常方便的给出了微软提供的API的各个版本之间的区别,比如下表是.NET 4和.NET 4.5的API变化总结.我们可以看到.NET 4 ...
- HTML5 File api 实现断点续传
目前市场上大多数的网站的断点上传都是需要安装浏览器插件的,本文就针对高级浏览器的环境下,通过HTML5 File api实现断点上传进行说明 一.实现文件多选 HTML5的<input>新 ...
- 异常Address already in use: JVM_Bind的处理
如题,Address already in use: JVM_Bind这个异常的意思就是说jvm被占用了 那么大家一般的解决情况都是重启一下eclipse , 结果还是不行,结果就只能重启电脑了. 对 ...
- SDL 实现透明悬浮窗
最近一直想用SDL实现弹幕功能,但是一直没法实现悬浮窗和透明背景功能. 在一个老外的博客上发现了思路:EthioProgrammer: Applying transparency using win3 ...
- Linux sed命令常用方法
sed也成stream editor,流编辑器,是Linux上常用的文本处理工具. 通用格式:sed 行范围 模式/RegExp/ 文件 模式: d 删除 p 打印符合条件的行 a \strin ...
- isAssignableFrom与instanceof的区别
1.isAssignableFrom针对的是class对象: 2.instanceof是实例. isAssignableFrom是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的 ...