gpt4 book ai didi

c# - 在 WPF DataGrid 上显示行号的简单方法

转载 作者:IT王子 更新时间:2023-10-29 04:23:04 39 4
gpt4 key购买 nike

我只想在我的 DataGrid 最左边的列中显示行号。有一些属性可以做到这一点吗?

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

最佳答案

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

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

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

当从源列表中添加或删除项目时,数字可能会暂时不同步。要解决此问题,请参阅此处的附加行为:
WPF 4 DataGrid: Getting the Row Number into the RowHeader

像这样使用

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

关于c# - 在 WPF DataGrid 上显示行号的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661998/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com