WPF 4 DataGrid:将行号获取到 RowHeader 中
我希望将行号放入 WPF 4 DataGrid 的 RowHeader 中,以便它有一个类似于 Excel 的列用于显示 DataGrid 的行号。
我在网上看到的解决方案建议向业务对象添加索引字段。这并不是一个真正的选择,因为 DataGrid 将被频繁使用,并且我们不想不断跟踪这些索引字段的更改。
多谢
I am looking to get the row number into the RowHeader of the WPF 4 DataGrid so it has an Excel-like column for the row numbers of the DataGrid.
The solution I've seen out there on the web suggests adding an index field to the business objects. This isn't really an option because the DataGrid will be getting resorted a lot and we don't want to have to keep track of changing these index fields constantly.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一种方法是将它们添加到
DataGrid
的 LoadingRow 事件中。更新
为了使其能够与 WPF Toolkit 中的 .NET 3.5 DataGrid 一起使用,需要进行一些修改。使用虚拟化时,索引仍然可以正确生成,但输出失败。对
RowHeaderTemplate
的以下修改修复了此编辑 2012-07-05
如果从源列表中添加或删除项目,则数字会不同步,直到滚动列表,从而再次调用
LoadingRow
。解决这个问题有点复杂,我现在能想到的最佳解决方案是保留上面的 LoadingRow 解决方案,并DataGridRows
DataGridRow
的索引以下是执行此操作的附加行为。像这样使用它
DisplayRowNumber
One way is to add them in the LoadingRow event for the
DataGrid
.Update
To get this to work with the .NET 3.5 DataGrid in WPF Toolkit a little modification is needed. The index is still generated correctly but the output fails when using virtualization. The following modification to the
RowHeaderTemplate
fixes thisEdit 2012-07-05
If items are added or removed from the source list then the numbers get out of sync until the list is scrolled so
LoadingRow
is called again. Working around this issue is a little more complex and the best solution I can think of right now is to keep theLoadingRow
solution above and alsodataGrid.ItemContainerGenerator.ItemsChanged
DataGridRows
in the visual treeDataGridRow
Here is an attached behavior which does this. Use it like this
DisplayRowNumber
编辑:显然滚动会更改索引,因此绑定不会像那样工作......
(看似)干净的模板解决方案:
Xaml:
转换器:
Edit: Apparently scrolling changes the index so the binding won't work like that...
A (seemingly) clean templating solution:
Xaml:
Converter:
如果添加或删除行,所有这些方法都将不起作用。在这种情况下,您应该刷新行索引。看看这个行为:
All of this approaches will not work if you add or remove rows. You should refresh row indexes in such cases. Look at this behavior:
@Fredrik Hedblad 的答案对我有用。谢谢!
我添加了另一个属性来获取“偏移”值,以便 DataGrid 可以从 0 或 1(或任何设置的值)开始。
要使用该行为,(注意“b”是命名空间)
修改后的类:
@Fredrik Hedblad's answer works for me. Thanks!
I added another property to get an "offset" value so the DataGrid can start from either 0 or 1 (or whatever is set).
To use the behavior, (note 'b' is the namespace)
The modified classes:
LoadingRowEvent 是这样触发的:
LoadingRowEvent is triggered by this:
另一个解决方案与上面的
IValueConverter
答案有些相似,但限制很少,但具有巨大的优势。ItemsSource
必须绑定到实现IList
的集合,因为需要IndexOf
方法来查找项目的索引。ObservableCollection
在这里很好。IndexOf
的比较方法不同。该解决方案的优点是独立于
DataGrid
的虚拟化类型。特别是默认虚拟化设置有效。因此它可以在相当大的虚拟化网格中正常工作。首先,我们需要一个多值转换器:
然后在 XAML 资源中注册它:
然后在
DataGrid
的行标题模板中使用该转换器:就是这样。
Another solution which is somewhat similar to the above answer with
IValueConverter
but have few restrictions and a huge advantage.ItemsSource
must be bound to the collection which implementsIList
bacause the methodIndexOf
is required to find the item's index.ObservableCollection<T>
is fine here.IndexOf
's approach to compare.The advantage of the solution is independency of
DataGrid
's virtualization type. In particular default virtualization settings works. So it will work properly with pretty large virtualized grids.First of all we need a multi-value converter:
Then register it in XAML resources:
Then use the converter in
DataGrid
's row header template:That's it.