Wpf – Programatically bringing a Datagrid row into view in WPF, MVVM


I would like to bring a row of my data grid into view programatically. I have a more than 100 rows. When I create a row(which I am doing by adding an item to a observable collection) I would like that new row to be selected and bring that into view. I was able to select the new row in my code but could not do the scrolling. More over I want the first cell of the row to be in edit mode so that the user can input text. I am following MVVM pattern for the application and would like to keep zero code in my views. How Can I achieve this?

Any help or suggestion will be appreciated….


This what I did in my XAML

<telerik:RadGridView ItemsSource="{Binding AllPartClasses}" 
                     SelectedItem="{Binding SelectedPartClassViewModel, Mode=TwoWay}"         
                     SelectionMode="Single" IsSynchronizedWithCurrentItem="True">

in my view model I did this

void AddNewPartClassExecute()
        PartClass newPartClass = new PartClass();
        PartClassViewModel tempPartClass = new PartClassViewModel(newPartClass);
        tempPartClass.IsInValid = true;
        SelectedPartClassViewModel = tempPartClass;

public PartClassViewModel SelectedPartClassViewModel 
            return _selectedPartClassViewModel;
            _selectedPartClassViewModel = value;

It did not work for me.

Best Solution

For the regular WPF DataGrid you can use ScrollIntoView. In your view hookup the SelectionChanged event to the following in your view code-behind cs file.

private void OnSelectionChanged( object sender, SelectionChangedEventArgs e )
    Selector selector = sender as Selector;
    DataGrid dataGrid = selector as DataGrid;
    if ( dataGrid != null && selector.SelectedItem != null && dataGrid.SelectedIndex >= 0 )
        dataGrid.ScrollIntoView( selector.SelectedItem );