检查 WPF DataGrid 中的可见行 [英] Check visible rows in a WPF DataGrid

查看:28
本文介绍了检查 WPF DataGrid 中的可见行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF DataGrid,当屏幕上有太多行要查看时,它会得到一个垂直滚动条.我想知道的是,当用户滚动时,是否有办法知道顶部可见行是什么.

I have a WPF DataGrid, which when there are too many rows to view on the screen it gets a vertical scrollbar. What I would like to know is if there is a way to know what the top visible row is when the user is scrolling.

理想情况下,我希望能够连接一个事件以了解用户何时滚动和滚动,检查顶部可见行以更新一些信息.

Ideally I would like to be able to wire up an event to know when the user is scrolling and on scroll, check what the top visible row is in order to update some information.

推荐答案

订阅 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).

这篇关于检查 WPF DataGrid 中的可见行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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