title: Adding item to ObservableCollection and make it selected in its bound ListCollectionView
body: <p>I have a ViewModel with an <code>ObservableCollection</code>, and a View using a Xaml-declared <code>CollectionViewSource</code>, which is bound to a <code>DataGrid.ItemsSource</code>.</p>
  
  <p>The ViewModel has a command to create a new object and add it to the <code>ObservableCollection</code>. Also in the ViewModel, I am handling the <code>CollectionChanged</code> event, so I "know" when an Add happens.</p>
  
  <p>My doubt is: </p>
  
  <blockquote>
    <p>how can I make the just-added object selected in the DataGrid?</p>
  </blockquote>
  
  <p>Notice that the addition and the changed event are all in the ViewModel, but the selection must happen in the View.</p>
  
  <p>I don't mind solutions based on code-behind.</p>
  
  <h2>ViewModel:</h2>
  
  <pre><code>public class TelaInicialViewModel : ViewModelBase
  {
      public ObservableCollection&lt;PacienteViewModel&gt; Pacientes { get; set; }
  
      IRepositório&lt;Paciente&gt; _repositório_pacientes = new RepositórioPaciente();
  
      // CONSTRUTOR
      public TelaInicialViewModel()
      {
          var pacientes = _repositório_pacientes.Items.Select(p =&gt; new PacienteViewModel(p));
          Pacientes = new ObservableCollection&lt;PacienteViewModel&gt;(pacientes);
          Pacientes.CollectionChanged += Pacientes_CollectionChanged;
      }
  
  
      void Pacientes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
          switch (e.Action)
          {
              case NotifyCollectionChangedAction.Add:
                  foreach (var added in e.NewItems)
                  {
                      var p = added as PacienteViewModel;
                      if (p != null)
                      {
                          var novoPaciente = p.GetModel();  /////////  HERE I HAVE IT!!
                          _repositório_pacientes.Adicionar(novoPaciente);
                      }
                  }
                  break;
              case NotifyCollectionChangedAction.Replace:
                  // ...
                  break;
          }
      }
  
  
      void AdicionarPaciente()
      {
          var formulárioPaciente = new FormulárioCriaçãoPaciente();
          PacienteViewModel novoPaciente = formulárioPaciente.ExibirDiálogo(new Paciente());
          if (novoPaciente != null)
              //////// HERE I HAVE IT!!!
              Pacientes.Add(novoPaciente);
      }
      public ICommand ComandoAdicionarPaciente { get { return new RelayCommand(AdicionarPaciente); } }
  }
  </code></pre>
  
  <h2>View (Xaml)</h2>
  
  <pre><code>&lt;UserControl.Resources&gt;
  
      &lt;CollectionViewSource
          x:Key="PacientesViewSource"
          Source="{Binding Pacientes}"
          Filter="PacientesViewSource_Filter"/&gt;
  
  &lt;/UserControl.Resources&gt;
  
  
  &lt;DockPanel&gt;
  
      &lt;DockPanel DockPanel.Dock="Top" Margin="0,10,0,0" &gt;
          &lt;TextBox x:Name="FiltroPacienteTextBox" Height="30"
              TextChanged="FiltroPacienteTextBox_TextChanged"
              DockPanel.Dock="Bottom" Margin="10,10,10,0"/&gt;
          &lt;Button x:Name="botaoNovoPaciente" Width="120" Content="Novo"
              HorizontalAlignment="Left" Height="30" Margin="10,0,0,0"
              Command="{Binding ComandoAdicionarPaciente}"/&gt;
      &lt;/DockPanel&gt;
  
      &lt;DataGrid x:Name="pacienteDataGrid" Margin="0,10,0,0"
          AutoGenerateColumns="False"
          CanUserAddRows="False" CanUserDeleteRows="False"
          CanUserReorderColumns="False" CanUserResizeRows="False"
          ItemsSource="{Binding Source={StaticResource PacientesViewSource}}"
          IsSynchronizedWithCurrentItem="True"&gt;
      &lt;/DataGrid&gt;
  
  &lt;/DockPanel&gt;        
  </code></pre>