Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have a WPF DataGrid that is inside of a Grid and has many rows, and thus has scroll bars. By default, when the user scrolls the DataGrid and the SelectedItem can sroll out of view. When this happens I would like the SelectedItem to change and become the first item that is in the datagrid view area so a SelectedItem is alway in view as the user scolls the DataGrid. I have done this in with WindowsForms and a DataGridView but have not figured out how to do this using a DataGrid in WPF. Any ideas as to the best approach for this? I found the ScrollViewer.ScrollChanged event for the DataGrid which seems like a good place to start, not sure where to go from there. I am using VB but C# code would be good to, I can translate.
Posted 11-Mar-13 12:41pm
Comments
joshrduncan2012 at 11-Mar-13 17:46pm
   
Can you show us what you have done and where you are stuck?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use the method System.Windows.Controls.Data.DataGrid.ScrollIntoView:
http://msdn.microsoft.com/en-us/library/cc673083%28v=vs.95%29.aspx[^].
 
Of course, don't call this method from ScrollChanged event handler, to avoid "infinite" recursion. Call it only when you add or remove items, or by user's request.
 
—SA
  Permalink  
v2
Comments
Member 8681301 at 11-Mar-13 18:38pm
   
Thanks,
I should have added though that I will not be adding or removing items from the DataGrid, it is read only. When the user scrolls the grid and the selected item goes out of view, I am trying to keep a selected item in view, the top row of the datagrid that is is view. I have a second datagrid that has a master details relationship set up with the first. The second datagrid show the details of the selecteditem in the first. When the user scrolls the first datagrid a selecteditem will always be in view and the second datagrid will update to the details of the selecteditem in the first as the user scrolls. The part I am having trouble with is keeping a selected item always in view in the first data grid as the user scrolls.
Sergey Alexandrovich Kryukov at 11-Mar-13 23:03pm
   
If you want to protect selected items from scrolling by the user, it means you would need to limit scrolling. It would be a huge abuse, don't you think so?..
—SA
Member 8681301 at 12-Mar-13 11:55am
   
I do not want to limit scrolling. If the currently SelectedItem moves out of view as the user scrolls, then SelectedItem would change to the top row that is still visable in the DataGrid so a SelectedItem is always visable or displayed. I have other DataGrid that are bound to the SelectedItem in the main DataGrid and they would update as the SelectedItem changes in the main DataGrid as the user scrolls the main DataGrid.
Sergey Alexandrovich Kryukov at 12-Mar-13 12:15pm
   
I think this is a bad idea. When a user selects something, normally she/he means it. Selection is more important than scroll as it affects logical functionality. Your action will confuse the users greatly. Just don't do it.
 
But you can always change selection. However, do you understand that a top row will generally be out of view as well? What's the use.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100