Click here to Skip to main content
12,301,374 members (60,892 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

66K views
16 bookmarked
Posted

ScrollIntoView for a DataGrid when using MVVM

, 8 Nov 2010 CPOL
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()
        {
            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).

<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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionBrilliant! Thank you a lot! Pin
Dmitry Leonov3-Sep-12 6:09
memberDmitry Leonov3-Sep-12 6:09 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 8 Nov 2010
Article Copyright 2010 by JeroenVonk
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid