显示在WPF DataGrid行数的简单方法 [英] Simple way to display row numbers on 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.
推荐答案
的一种方法是增加他们在LoadingRow事件为DataGrid
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中:获取行号到的rowHeader
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屋!