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

datagridmvvmwpf

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….

Update:

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;
        AllPartClasses.Add(tempPartClass);
        SelectedPartClassViewModel = tempPartClass;
        Global.DbContext.PartClasses.AddObject(newPartClass);

        //OnPropertyChanged("AllPartClasses");
    }
public PartClassViewModel SelectedPartClassViewModel 
    { 
        get
        {  
            return _selectedPartClassViewModel;
        }
        set
        {
            _selectedPartClassViewModel = value;
            OnPropertyChanged("SelectedPartClassViewModel");
        }
    }

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