在 WPF DataGrid 上显示行号的简单方法 [英] Simple way to display row numbers on WPF DataGrid

查看:31
本文介绍了在 WPF DataGrid 上显示行号的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在我的 DataGrid 的最左侧列中显示行号.有什么属性可以做到这一点吗?

I just want to display row numbers in the left-most column of my DataGrid. Is there some attribute to do this?

请记住,这不是我的表的主键.当对列进行排序时,我不希望这些行号随行一起移动.我基本上想要一个运行计数.它甚至不需要标题.

Keep in mind, this isn't a primary key for my table. I don't want these row numbers to move with their rows when a column is sorted. I basically want a running count. It doesn't even need to have a header.

推荐答案

一种方法是将它们添加到 DataGrid 的 LoadingRow 事件中

One way is to add them in the LoadingRow event for the DataGrid

<DataGrid Name="DataGrid" LoadingRow="DataGrid_LoadingRow" ...

void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.Header = (e.Row.GetIndex()).ToString(); 
}

在源列表中添加或删除项目时,数字可能会暂时不同步.要解决此问题,请参阅此处的附加行为:
WPF 4 DataGrid:获取行号进入行标题

When items are added or removed from the source list then the numbers can get out of sync for a while. For a fix to this, see the attached behavior here:
WPF 4 DataGrid: Getting the Row Number into the RowHeader

可以这样使用

<DataGrid ItemsSource="{Binding ...}"
          behaviors:DataGridBehavior.DisplayRowNumber="True"> 

这篇关于在 WPF DataGrid 上显示行号的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆