Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check visible rows in a 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.

like image 599
ctrlalt313373 Avatar asked Mar 18 '09 13:03

ctrlalt313373


1 Answers

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

like image 57
skybluecodeflier Avatar answered Sep 19 '22 12:09

skybluecodeflier