tags:

views:

1565

answers:

2

I have a wpf datagrid (the one from the wpf toolkit), which when there are too many rows to view on the screen it gets a vertical scroll bar. 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 to 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.

A: 

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
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.
David Osborn
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.
David Osborn
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
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.
David Osborn
+1  A: 

By the follow way , I Got it.

  1. Get The HorizontalScrollBar sub class control's instance (mHorizontalScrollBar) .

  2. Get the Total Item Count

    nTotalCount = DataGrid1.Items.Count ;

  3. Get First Visible row index

    nFirstVisibleRow = mHorizontalScrollBar.Value ;

  4. Get Last visible row Index nLastVisibleRow = nFirstVisibleRow + nTotalCount - mHorizontalScrollBar.Maximum ;