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的方式)的更多相关文章

  1. WPF DataGrid自动生成行号

      在使用WPF进行应用程序的开发时,经常会为DataGrid生成行号,这里主要介绍一下生成行号的方法.通常有三种方法,这里主要介绍其中的两种,另一种简单提一下. 1. 直接在LoadingRow事件 ...

  2. WPF DataGrid 绑定DataSet数据 自动生成行号

    1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...

  3. Dev的GridView中如何自动生成行号

    这里提供一个方法,使用简单,只需将GridView传入,即可自动生成行号 public static void SetRowNumberIndicator(GridView gridView) { g ...

  4. 用ClientDataSet更新数据表,怎样自动生成行号? [问题点数:40分]

    ClientDataSet.First;while not ClientDataSet.eof dobegin  ClientDataSet.edit;  ClientDataSet.FieldByN ...

  5. WPF DataGrid自动生成序号

    需求和效果 应用WPF技术进行开发的时候,大多都会遇到给DataGrid添加序号的问题,今天分享一下查阅了很多stackoverflow的文章后,总结和改进过来的方法,先看一下效果图,文末附Demo下 ...

  6. Flex 生成行号

    private function formatIndexNumber(item:Object, colum:Object):String {      return indexNumLabelFun( ...

  7. wpf DataGrid加载行号

    <DataGrid Name="tkdg" HorizontalContentAlignment="Center" AutoGenerateColumns ...

  8. WPF DataGrid自动生成列

    <Window x:Class="DataGridExam.MainWindow"        xmlns="http://schemas.microsoft.c ...

  9. Wpf DataGrid 自动滚动到最后一行

    if (mainDataGrid.Items.Count > 0) { var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as De ...

随机推荐

  1. [HTTP2] HTTP1 probs and HTTP2 saves

    1. HOL (HEADS of LINE BLOCKING) Too many requests in the header tag. Broswer can allow 6 reuqest to ...

  2. cocos2dx jsb 在IOS与安卓下的一些不同之处

    cocos2dx版本 2.1.4 1:字体大小 cocosbuilder publish后应该注意IOS和安卓目录下的字体文件的规格是否齐全: 2:cc.LabelTTF 在ios下,cc.Label ...

  3. InnoDB的redo日志管理---饶珑辉

    http://raolonghui.com/2015/06/24/innodb%E7%9A%84redo%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86/#comment-11

  4. 【转】Cocos2d-x 2.0 拖尾效果深入分析

    Cocos2d-x 2.0 拖尾效果深入分析 另:本章所用Cocos2d-x版本为: cocos2d-2.0-x-2.0.2@ Aug 30 2012 http://cn.cocos2d-x.org/ ...

  5. 自定义ContentProvider

    ContentProvider作为安卓的四大组件之一,在看开发中用到的频率远不如其他三个,以至于我都把这个东西给忘了,最近由于工作原因,不得不重新拾起来总结一下,那么今天就来说说自定义ContentP ...

  6. Android(java)学习笔记153:layout_weight使用注意事项

    1. android:layout_weight使用说明: layout_weight是权重的意思,也就是各个控件所占的比重,用在LinearLayout布局中.当我们使用layout_weight的 ...

  7. Nginx+Tomcat+memcached负载均衡实现session共享

    http://blog.csdn.net/love_ubuntu/article/details/8464983 1.  安装各个软件不用说了. 2.  到tomcat的安装目录lib中,加入: me ...

  8. sqlServer 求当前周的第一天和最后一天,当前月的第一天和最后一天,前三个月的第一天和今天

    ---当前周的第一天 ),DATEADD(day,-(DATEPART(weekday,GETDATE())-),GETDATE()) , )as'周一', CONVERT(varchar(),DAT ...

  9. nyoj832 合并游戏(状态压缩DP)

    题意 : n个石子, 给你一个n*n矩阵, A[i][j]表示第i个和第j个合并蹦出的金币值, 合并完石子 j 消失.求合并所有石子后,所得的最大金币数. 分析 :     1. 题中给的数据范围   ...

  10. SQL Cursor 游标的使用

    DECLARE @name VARCHAR(50)  --声明游标 DECLARE cursor_VAA1 CURSOR FOR SELECT VAA05 FROM VAA1 --打开游标 OPEN ...