WPF DataGrid 自动生成行号的方法(通过修改RowHeaderTemplate的方式)
2013-07-15 15:56
399 查看
WPF中的DataGrid自动生成行号的方法有很多,这里记录了一种通过修改 RowHeaderTemplate的方式来生成行号:
方法一:
xaml界面:
其中的Converter代码:
这样就ok了。。
这种方法的好处是方便更好的自定义表头样式。
还有一个作者也觉得好用的方法,是通过为DataGrid添加一个Behavior,但是这个方法不好扩展表头的样式。
方法二:
然后在DataGrid上添加该Behavior就可以了。
但是这个方法就只能显示出来,要是表头想在数字后面加一个图片,这就没第一种方法好用了。
欢迎转载,请注明来自Leaco的博客:/article/5301303.html
方法一:
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() + 1; } 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()+1; }; } }
然后在DataGrid上添加该Behavior就可以了。
<DataGrid Behavior:DataGridShowRowIndexBehavior.ShowRowIndex="True"/>
但是这个方法就只能显示出来,要是表头想在数字后面加一个图片,这就没第一种方法好用了。
欢迎转载,请注明来自Leaco的博客:/article/5301303.html
相关文章推荐
- WPF让DataGrid自动生成行号
- WPF DataGrid 绑定DataSet数据 自动生成行号
- WPF DataGrid自动生成行号
- WPF:DataGrid 自动生成行号
- WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法
- WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
- 关于hibernate通过注解方式自动生成表时字段的顺序问题
- Hibernate自动生成的DAO的修改方法
- WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
- 如何手动修改或增加word2007自动生成的目录的链接(修改域代码方式):
- Linux下自动修改用户密码的方法(直接通过命令而不是在终端输入密码)
- springboot 通过代码自动生成pid的方法
- WPF非轮询方式更新数据库变化SqlDependency(数据库修改前台自动更新)
- 【转载】wpf DataGrid自动显示行号
- Mybatis 通过配置文件generatorConfig.xml生成文件--main方法执行方式
- C#下用zedGraph生成大量数据统计图表的方法(通过修改一些源码)
- Mybatis Generator的model生成中文注释,支持oracle和mysql(通过修改源码的方式来实现)
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
- Hibernate自动生成的DAO的修改方法
- oracle11g 通过修改配置文件方式连接远程数据库的方法