且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查 WPF DataGrid 中的可见行

更新时间:2023-12-06 17:28:52

订阅 DataGrid 的 ScrollViewer 上的 ScrollViewer.ScrollChanged 事件怎么样?它的事件参数非常广泛,描述了 ScrollViewer 移动了多少以及它的新垂直偏移量是多少.另外,根据 MSDN:

How about subscribing to the ScrollViewer.ScrollChanged event on the DataGrid's ScrollViewer? The event arguments for it are pretty extensive, describing how much the ScrollViewer moved and what its new Vertical offset is. Also, according to MSDN:

如果 CanContentScroll 为 true,则 ExtentHeight、ScrollableHeight、ViewportHeight 和 VerticalOffset 属性的值是项目数.如果 CanContentScroll 为 false,则这些属性的值是与设备无关的像素.

If CanContentScroll is true, the values of the ExtentHeight, ScrollableHeight, ViewportHeight, and VerticalOffset properties are number of items. If CanContentScroll is false, the values of these properties are Device Independent Pixels.

CanContentScroll 确实适用于 DataGrid 的 ScrollViewer.

CanContentScroll is indeed the case for the ScrollViewer for a DataGrid.

您所要做的就是找到 ScrollViewer:

All you have to do is find the ScrollViewer:

ScrollViewer scrollview = FindVisualChild<ScrollViewer>(dataGrid);

使用可以在各个地方找到的 FindVisualChild 的实现(例如:在其中找到控制WPF 项目控制).

using an implementation of FindVisualChild that can be found in various places (like here: Finding control within WPF itemscontrol).