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. 用WebCollector爬取站点的图片

    用WebCollector爬取整站图片,仅仅须要遍历整站页面.然后将URL为.jpg.gif的页面(文件)保存到本地就可以. 比如我们爬取一个美食站点,获取里面全部的图片: import cn.edu ...

  2. Android解析Json速度最快的库:json-smart

    场景描写叙述: 本文仅验证了在安卓环境下使用Json的Key作为反序列化条件的解析速度.结论是解析速度最快的不是阿里的fastjson,也不是Google的Gson,而是json-smart. And ...

  3. 分布式数据库中间件–(3) Cobar对简单select命令的处理过程

    友情提示:非原文链接可能会影响您的阅读体验,欢迎查看原文.(http://blog.geekcome.com) 原文地址:http://blog.geekcome.com/archives/284 在 ...

  4. android webview内容压线问题解决方法

    最近在使用webview做页面开发,项目上要求webview在获取到焦点的时候需要有边框线,于是添加上了webview的选中效果,但是出现了网页中的内容压选中框的情况.之后给webview添加padd ...

  5. 解决windows server 2003不认U盘或移动硬盘

    解决windows server 2003不认U盘或移动硬盘1.进入命令提示符环境(也就是DOS) 2.进入DISKPART程序 3.输入AUTOMOUNT ENABLE指令 4.OK,下次USB硬盘 ...

  6. [TypeScript] 0.First Example

    Create a greeter.ts file: class Student { fullname : string; constructor(public firstname, public mi ...

  7. C# - 系统类 - Object类

    Object类 ns:System 此类是所有.NET Framework中的类的基类 Type类就派生自Object类 C#提供了object关键字来表示一个类实例的类型 而无需使用Object作为 ...

  8. 对Cookie进行增删改查

    public class CookieServletDemo extends HttpServlet { public void doGet(HttpServletRequest request, H ...

  9. HDU1010(bfs)

    #include <stdio.h>#include <iostream>#include <string.h>#include <stdlib.h>u ...

  10. 获取随机颜色js

    获取随机颜色方法一: function randomColor1() { var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16); i ...