Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Tip/Trick

ScrollIntoView for a DataGrid when using MVVM

Rate me:
Please Sign up or sign in to vote.
5.00/5 (22 votes)
8 Nov 2010CPOL 78.2K   19   12
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.)

C#
namespace MVVM.Framework
{public class ScrollIntoViewBehavior : Behavior<DataGrid>
    {
        protected override void OnAttached()
        {
            base.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.Dispatcher.BeginInvoke(delegate
                    {
                        grid.UpdateLayout();
                        grid.ScrollIntoView(grid.SelectedItem, null);
                    });
                }
            }
        }
        protected override void OnDetaching()
        {
            base.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).

XML
<sdk:DataGrid AutoGenerateColumns="true" ItemsSource="{Binding ObjectList}" SelectedItem="{Binding SelectedObject, Mode=TwoWay}" Name="dataGridObjects">
    <i:Interaction.Behaviors>
        <fw:ScrollIntoViewBehavior/>
    </i:Interaction.Behaviors>
</sdk:DataGrid>

License

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


Written By
Software Developer (Senior) Inter Access BV
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionScrollIntoView Pin
Member 961796310-Jun-19 5:17
Member 961796310-Jun-19 5:17 
QuestionSweet Pin
Cornelius Henning19-Dec-16 9:56
professionalCornelius Henning19-Dec-16 9:56 
PraiseThank you very much! Pin
censation30-Jun-16 23:36
censation30-Jun-16 23:36 
GeneralThanks for the code. Had to do this small change to make it work on .Net 4.0 Pin
Ildefonso Zanette28-May-15 3:37
Ildefonso Zanette28-May-15 3:37 
QuestionThe attachable property 'Behaviors' was not found in type 'Interaction'. Pin
Paul Cooper3-Dec-13 22:46
Paul Cooper3-Dec-13 22:46 
QuestionScroll position Pin
Anne Jan Pool16-Oct-13 23:39
Anne Jan Pool16-Oct-13 23:39 
QuestionFine concept, 5***** Pin
Member 87070899-Oct-13 1:53
Member 87070899-Oct-13 1:53 
QuestionBrilliant! Thank you a lot! Pin
Dmitry Leonov3-Sep-12 6:09
Dmitry Leonov3-Sep-12 6:09 
GeneralExcellent! Thank you- this saved me a lot of time and allow... Pin
MonAccount15-Sep-11 5:24
MonAccount15-Sep-11 5:24 
GeneralReason for my vote of 5 Exactly what I needed. Nice work. Pin
scottfm15-Jul-11 2:47
scottfm15-Jul-11 2:47 
GeneralSweet. I got this to work for a listBox too! Thanks! Pin
Mr. Sharps28-Apr-11 20:01
Mr. Sharps28-Apr-11 20:01 
GeneralReason for my vote of 5 Neat tip! Pin
Mamta D9-Nov-10 17:00
Mamta D9-Nov-10 17:00 

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.