title: how to save selected index in datagrid and set selected item back when the user reloads the page?
body: <p>I have a DataGrid in a WPF project that is filled using MVVM.
  When the user selects a row and then clicks the edit button a window opens.
  My problem it is that, when the user comes back to DataGrid, the selected item was changed and set to first row!</p>
  
  <p>I wrote this code in view model :</p>
  
  <pre><code>UserInfoDAL userInfoDAL;
  
      public UserInfoListVM()
      {
  
          userInfoDAL = new UserInfoDAL();
          UserInfoCollection = new ObservableCollection&lt;UserInfo&gt;();
          UserInfoModelCurrent = new UserInfo();
  
          UserInfoSelect();
      }
     private ObservableCollection&lt;UserInfo&gt; _UserInfoCollection;
      public ObservableCollection&lt;UserInfo&gt; UserInfoCollection
      {
          get { return this._UserInfoCollection; }
          set
          {
              this._UserInfoCollection = value;
              base.OnPropertyChanged("UserInfoCollection");
          }
      }
      private int _selectedIndex;
      public int SelectedIndex
      {
          get { return _selectedIndex; }
          set
          {
              _selectedIndex = value;
              base.OnPropertyChanged("SelectedIndex");
          }
      }
      public UserInfo UserInfoModelCurrent
      {
          get;
          set;
      }public void UserInfoSelect()
      {
          SelectedIndex = _selectedIndex;
          string _errorMessage = "";
  
          ObservableCollection&lt;UserInfo&gt; _UserInfoModelCollection = new ObservableCollection&lt;UserInfo&gt;();
          _UserInfoModelCollection = userInfoDAL.UserInfoSelect(null, out _errorMessage);
          if (!string.IsNullOrEmpty(errorMessage))
          {
              MessageBox.Show(errorMessage, "خطا", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.OK);
          }
          else
          {
              UserInfoCollection = _UserInfoModelCollection;
          }
          Messenger.Default.Send(this, "UserInfoListNotify");
  
  
      }
  </code></pre>
  
  <p>in xaml:</p>
  
  <pre><code>&lt;DataGrid 
      x:Name="GrdUserInfo" 
      Grid.Row="1" 
      ItemsSource="{Binding UserInfoCollection}" 
      MouseRightButtonDown="GrdUserInfo_MouseRightButtonDown" 
      LoadingRow="GrdUserInfo_LoadingRow" 
      AutoGeneratingColumn="GrdUserInfo_AutoGeneratingColumn" 
      SelectedItem="{Binding UserInfoModelCurrent,Mode=TwoWay}" 
      Loaded="GrdUserInfo_Loaded" 
      SelectionChanged="GrdUserInfo_SelectionChanged" 
      SelectedIndex="{Binding SelectedIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChange‌​d}" 
      &gt;
  </code></pre>
  
  <p>in codebehind:</p>
  
  <pre><code>     public UserInfoListView()
      {
          InitializeComponent();
          userInfoListViewModel = new UserInfoListVM();
  
          this.DataContext = userInfoListViewModel;
  
          Messenger.Default.Register&lt;UserInfoListVM&gt;(this, "UserInfoListNotify", x =&gt;
          {
              DataGridSetting.DataGridPreparation(TabName, ref GrdUserInfo, userInfoListViewModel.UserInfoCollection, SelectedIndexRow);
          }
          );
      }
      public bool RestoreGridDefault = false;
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          Window window = Window.GetWindow(this);
          window.Closing += window_Closing;
          window.KeyUp += Window_KeyDown;
      }
  
      private void GrdUserInfo_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
      {
          DataGridSetting.DataGridSetColumnPrperty(ref e);
         // e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
      }
      private void GrdUserInfo_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
      {
          DataGridSetting.DataGridCreateContexMenu(ref GrdUserInfo, ref RestoreGridDefault, "userInfo", e);
      }
      private void GrdUserInfo_LoadingRow(object sender, DataGridRowEventArgs e)
      {
          e.Row.Header = (e.Row.GetIndex()+1).ToString();
  
      }
      private void GrdUserInfo_Loaded(object sender, RoutedEventArgs e)
      {
          DataGridSetting.DataGridPreparation(TabName, ref GrdUserInfo, userInfoListViewModel.UserInfoCollection, SelectedIndexRow);
      }
  
      private void GrdUserInfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          userInfoListViewModel.SelectedIndex =  GrdUserInfo.SelectedIndex;
      }
  </code></pre>
  
  <p>but its not solving my problem :(</p>