Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
joshrduncan2012 11-Mar-13 17:46pm    
Can you show us what you have done and where you are stuck?

1 solution

You can use the method System.Windows.Controls.Data.DataGrid.ScrollIntoView:[^].

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.

Share this answer
Member 8681301 11-Mar-13 18:38pm    
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 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?..
Member 8681301 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 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.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900