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.
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:
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 is indeed the case for the ScrollViewer for a DataGrid.
All you have to do is find the ScrollViewer:
ScrollViewer scrollview = FindVisualChild<ScrollViewer>(dataGrid);
using an implementation of FindVisualChild that can be found in various places (like here: Finding control within WPF itemscontrol).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With