8

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.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
ctrlalt313373
  • 3,935
  • 7
  • 36
  • 40

5 Answers5

3

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

Community
  • 1
  • 1
skybluecodeflier
  • 1,339
  • 13
  • 26
2

Detecting scrolling is as easy as

<DataGrid ScrollViewer.ScrollChanged="DataGrid_ScrollChanged" />

Now you must get ScrollViewer instance:

void DataGrid_ScrollChanged(object sender, RoutedEventArgs e)
{
    var scroll = FindVisualChild<ScrollViewer>((DependencyObject)sender);
    ...
}

(Not sure where is origin of FindVisualChild, but there are plenty of implementations, e.g. here)

And then you can

int firstRow = (int)scroll.VerticalOffset;
int lastRow = (int)scroll.VerticalOffset + (int)scroll.ViewportHeight + 1;
Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 2
    The correct index of the last row is calculated using (int)scroll.VerticalOffset + (int)scroll.ViewportHeight - 1; – Parisa Aug 20 '18 at 12:33
2

Using the following method worked for me:

// mHorizontalScrollBar is the HorizontalScrollBar subclass control's instance

// Get the total item count
nTotalCount = DataGrid1.Items.Count; 

// Get the first visible row index 
nFirstVisibleRow = mHorizontalScrollBar.Value;

// Get the last visible row index
nLastVisibleRow = nFirstVisibleRow + nTotalCount - mHorizontalScrollBar.Maximum;
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • @DannyBeckett, edit makes the answer completely useless (or, well, very unclear), reverting back to original answer. – Sinatr Sep 21 '16 at 13:51
  • 1
    @Sinatr I disagree and I've rolled back your change. If you still disagree, please post a question Meta Stack Exchange instead of starting a rollback war. – Danny Beckett Sep 21 '16 at 14:16
  • @DannyBeckett, before it was a clean step-by-step guide (I agree with somewhat bad language) and I am sure upvotes were given at that time. I run now into same problem and when trying this answer in current state I fail to make it working and even downvoted it. Then I spot your edit and read original answer, which is waaay more clear. Let's leave it as it is. Future readers will see my comment anyway (but not all of them will be able to see revisions I guess, not my problem ;). – Sinatr Sep 21 '16 at 14:50
  • 3
    How do you get `mHorizontalScrollBar`? – Josh Noe Jun 02 '17 at 15:43
0

It's sort of an overcomplicated way of doing it, but it may work. First, subclass DataGridRowsPresenter and override the OnViewportOffsetChanged method. Then, duplicate the standard control template for the datagrid, and replace the DataGridRowsPresenter with your own. I leave the details of hit testing for a row relative to the viewport up to you ;-).

What are you trying to accomplish, specifically? Maybe we can come up with a better way, as this may be very brittle and requires a bunch of extra work (i.e. keeping the control template in sync if they update it).

Bob King
  • 25,372
  • 6
  • 54
  • 66
  • Thanks. I've gotten around it by just scaling up what is in the grid (fonts mainly) using animations. Not quite as simple, but basically what I wanted. – ctrlalt313373 Mar 19 '09 at 21:12
  • I ran into an issue with scaling the font, that when scaling the font down the columns won't auto resize so they are still width of the bigger font size. – ctrlalt313373 Mar 20 '09 at 02:39
  • There's sadly a bug in the current build of the WPFToolkit that makes auto resize columns work strangely when they're mixed with Star sized columns. We see that behavior as well, and we've just never bothered to fix it. – Bob King Mar 20 '09 at 12:26
  • Thanks, actually if my above comments make no sense it was because I've been working on two separate issues, one with trying to figure out the top row and the other with zooming a datagrid. I gave up on the top row thing and told the user they would need to adjust the UI. – ctrlalt313373 Mar 21 '09 at 02:19
0

Maybe a late answer, maybe not the best way but one of the simplest.

    public void SelectPatient(string patientID)
    {
        var item = PatientList.FirstOrDefault(ro => ro.Pati_ID == patientID);
        if (item != null)
        {
            grdPatients.SelectedItem = item;
            grdPatients.ScrollIntoView(grdPatients.Items[grdPatients.Items.Count - 1]); //Scrolls to the last row
            grdPatients.UpdateLayout();
            grdPatients.ScrollIntoView(grdPatients.SelectedItem);
            grdPatients.UpdateLayout();
        }
    }
Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32