Click here to Skip to main content
13,355,311 members (58,784 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 8 Nov 2010

ScrollIntoView for a DataGrid when using MVVM

, 8 Nov 2010
Rate this:
Please Sign up or sign in to vote.
I just ran into a small problem with a databound DataGrid in Silverlight. If the SelectedItem is changed by the ViewModel, the DataGrid does not scroll to the SelectedItem.
I created a behavior to do this for me. So now if the Selection of the DataGrid is changed, the DataGrid scolls to the selecteditem. (It used the ScrollIntoView method of the DataGrid.)

namespace MVVM.Framework
{public class ScrollIntoViewBehavior : Behavior<DataGrid>
        protected override void OnAttached()
            this.AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
        void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
            if (sender is DataGrid)
                DataGrid grid = (sender as DataGrid);
                if (grid.SelectedItem != null)
                        grid.ScrollIntoView(grid.SelectedItem, null);
        protected override void OnDetaching()
            this.AssociatedObject.SelectionChanged -=
                new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);

To use this in XAML: (make sure you have included xmlns:fw="clr-namespace:MVVM.Framework" in your header).

<sdk:DataGrid AutoGenerateColumns="true" ItemsSource="{Binding ObjectList}" SelectedItem="{Binding SelectedObject, Mode=TwoWay}" Name="dataGridObjects">


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


About the Author

Software Developer (Senior) Inter Access BV
Netherlands Netherlands
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionSweet Pin
Cornelius Henning19-Dec-16 10:56
professionalCornelius Henning19-Dec-16 10:56 
PraiseThank you very much! Pin
Member 114118441-Jul-16 0:36
memberMember 114118441-Jul-16 0:36 
GeneralThanks for the code. Had to do this small change to make it work on .Net 4.0 Pin
Ildefonso Zanette28-May-15 4:37
memberIldefonso Zanette28-May-15 4:37 
QuestionThe attachable property 'Behaviors' was not found in type 'Interaction'. Pin
Paul Cooper3-Dec-13 23:46
memberPaul Cooper3-Dec-13 23:46 
QuestionScroll position Pin
Anne Jan Pool17-Oct-13 0:39
memberAnne Jan Pool17-Oct-13 0:39 
QuestionFine concept, 5***** Pin
Member 87070899-Oct-13 2:53
memberMember 87070899-Oct-13 2:53 
QuestionBrilliant! Thank you a lot! Pin
Dmitry Leonov3-Sep-12 7:09
memberDmitry Leonov3-Sep-12 7:09 
GeneralExcellent! Thank you- this saved me a lot of time and allow... Pin
MonAccount15-Sep-11 6:24
memberMonAccount15-Sep-11 6:24 
GeneralReason for my vote of 5 Exactly what I needed. Nice work. Pin
scottfm15-Jul-11 3:47
memberscottfm15-Jul-11 3:47 
GeneralSweet. I got this to work for a listBox too! Thanks! Pin
Mr. Sharps28-Apr-11 21:01
memberMr. Sharps28-Apr-11 21:01 
GeneralReason for my vote of 5 Neat tip! Pin
mamta_m_d9-Nov-10 18:00
membermamta_m_d9-Nov-10 18:00 
Reason for my vote of 5
Neat tip!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by JeroenVonk
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid