WPF DataGrid 自动生成行号的方法(通过修改RowHeaderTemplate的方式)
WPF中的DataGrid自动生成行号的方法有很多,这里记录了一种通过修改 RowHeaderTemplate的方式来生成行号:
方法一:
xaml界面:
<Window
...
xmlns:local="clr-namespace:Test"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Window.Resources>
<local:RowToIndexConv x:Key="RowToIndexConv"/>
</Window.Resources>
<DataGrid ItemsSource="{Binding GridData}">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource RowToIndexConv}}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
</Window>
其中的Converter代码:
public class RowToIndexConv : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DataGridRow row = value as DataGridRow;
return row.GetIndex() + ;
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这样就ok了。。
这种方法的好处是方便更好的自定义表头样式。
还有一个作者也觉得好用的方法,是通过为DataGrid添加一个Behavior,但是这个方法不好扩展表头的样式。
方法二:
public class DataGridShowRowIndexBehavior
{
public static bool GetShwoRowIndex(DependencyObject obj)
{
return (bool)obj.GetValue(ShowRowIndexProperty);
} public static void SetShowRowIndex(DependencyObject obj,bool value)
{
obj.SetValue(ShowRowIndexProperty,value);
} public static readonly DependencyProperty ShowRowIndexProperty = DependencyProperty.RegisterAttached("ShowRowIndex",typeof(bool),typeof(DataGridShowRowIndexBrhavior),new UIPropertyMetaData(false,ShowRowIndexPropertyChanged)); private static void ShowRowIndexPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
var dataGrid= d as DataGrid;
if (dataGrid==null) return ;
dataGrid.LoadingRow+=
delegate(object sender,DataGridRowEventArgs e1)
{
e1.Row.Header=e1.Row.GetIndex()+;
};
} }
然后在DataGrid上添加该Behavior就可以了。
<DataGrid Behavior:DataGridShowRowIndexBehavior.ShowRowIndex="True"/>
但是这个方法就只能显示出来,要是表头想在数字后面加一个图片,这就没第一种方法好用了。
欢迎转载,请注明来自Leaco的博客:http://www.cnblogs.com/Leaco/p/3191294.html
WPF DataGrid 自动生成行号的方法(通过修改RowHeaderTemplate的方式)的更多相关文章
- WPF DataGrid自动生成行号
在使用WPF进行应用程序的开发时,经常会为DataGrid生成行号,这里主要介绍一下生成行号的方法.通常有三种方法,这里主要介绍其中的两种,另一种简单提一下. 1. 直接在LoadingRow事件 ...
- WPF DataGrid 绑定DataSet数据 自动生成行号
1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...
- Dev的GridView中如何自动生成行号
这里提供一个方法,使用简单,只需将GridView传入,即可自动生成行号 public static void SetRowNumberIndicator(GridView gridView) { g ...
- 用ClientDataSet更新数据表,怎样自动生成行号? [问题点数:40分]
ClientDataSet.First;while not ClientDataSet.eof dobegin ClientDataSet.edit; ClientDataSet.FieldByN ...
- WPF DataGrid自动生成序号
需求和效果 应用WPF技术进行开发的时候,大多都会遇到给DataGrid添加序号的问题,今天分享一下查阅了很多stackoverflow的文章后,总结和改进过来的方法,先看一下效果图,文末附Demo下 ...
- Flex 生成行号
private function formatIndexNumber(item:Object, colum:Object):String { return indexNumLabelFun( ...
- wpf DataGrid加载行号
<DataGrid Name="tkdg" HorizontalContentAlignment="Center" AutoGenerateColumns ...
- WPF DataGrid自动生成列
<Window x:Class="DataGridExam.MainWindow" xmlns="http://schemas.microsoft.c ...
- Wpf DataGrid 自动滚动到最后一行
if (mainDataGrid.Items.Count > 0) { var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as De ...
随机推荐
- ubuntu13.04 Thinkpad W520安装nvidia显卡驱动
Ubuntu13.04 amd64 Thinkpad W520安装Nvidia显卡驱动 曾经在ubuntu11.10上成功安装Nvidia显卡驱动.但是自从机器(Thinkpad W520)更新到13 ...
- [Webpack 2] Optimize React size and performance with Webpack production plugins
You can fine tune several webpack plugins to make your bundle as small as it can be for your specifi ...
- php连接oracle
1.安装oracle客户端,不管是32位还是64位:当系统是32位的时候,要装32位的客户端,PL/SQL才能给连上数据库 2.php.ini中对应的oracle相关扩展打开.php_openssl. ...
- cglib源码分析(一): 缓存和KEY
cglib是一个java 字节码的生成工具,它是对asm的进一步封装,提供了一系列class generator.研究cglib主要是因为它也提供了动态代理功能,这点和jdk的动态代理类似. 一. C ...
- Asp.Net分页存储过程
SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in (select top (当前的 ...
- oracle EBS 基于Host并发程序的开发(转)
参考此编文章 http://www.doc88.com/p-0972680953307.html http://www.cnblogs.com/benio/archive/2011/06/10/207 ...
- samba服务器与远程登录ssh
作者:相思羽 出处:http://www.cnblogs.com/xiang-siyu 欢迎转载,也请保留这段声明.谢谢! deepin安装与配置samba服务器 安装 apt-get insta ...
- .net+easyui系列--按钮
easyui提供了各种按钮样式,包括搜索.新增.保存.删除等 <a id="btn" href="#" class="easyui-linkbu ...
- "只能在执行Render()的过程中调用RegisterForEventValidation" 解决方案
开发中遇到令人蛋疼的问题: 只能在执行Render()的过程中调用RegisterForEventValidation 当出现的异常的提示: 异常详细信息: System.InvalidOperati ...
- struts2学生信息管理系统篇章①
最近在看java1234分享的一个Struts2的学生信息管理系统的知识.因为本身java没什么底子.所以就没有什么好的技术去解决问题.一直在百度,不懂就百度.这样子下来其实也能学到一些东西,过阵子等 ...