title: How can I highlight a DataGrid row programmatically from the ViewModel alone?
body: <p><strong>The SelectedItem property binding is not causing its DataGrid row to be highlighted on initial load.</strong></p>
  
  <p>I have a DataGrid with a binding on SelectedItem that is not getting highlighted until I click it again. I think I have an order of operations problem--coming from the fact that all the ViewModel code runs before the Views get rendered. It works fine once I click a row (even the same one that's already in the SelectedAccount prop), but I need to able to highlight a row from the VM.</p>
  
  <p>I can easily verify the SelectedAccount property is not null because there are other ViewModels that display it's values via PubSubEvents. </p>
  
  <p>I've tried several solutions methods, and the only way I've been able to get it to work so far just feels dirty:</p>
  
  <pre><code>using ApplicationName.UI.ViewModels;
  public partial class AccountsView : UserControl
  {
      public AccountsView()
      {
          InitializeComponent();
          Loaded += AccountsView_Loaded;
      }
  
      private void AccountsView_Loaded(object sender, RoutedEventArgs e)
      {
          AccountsViewModel viewModel = (AccountsViewModel)DataContext;
          AccountsDataGrid.SelectedItem = viewModel.SelectedAccount;
          AccountsDataGrid.Focus();
          UpdateLayout();
      }   
  }
  </code></pre>
  
  <p>I don't like this because it causes the <code>OnPropertyChanged</code> event to fire twice, once before the views load, and and again after the above hack. This triggers a SQL call, so I want to avoid that. I also thought the point of MVVM was to decouple the view from the viewmodel, but maybe I'm not understanding that as well as I thought.</p>
  
  <p>Here's the XAML for the DataGrid:</p>
  
  <pre><code>&lt;ScrollViewer Grid.Row="1"
                Grid.Column="0"
                Grid.ColumnSpan="3"
                Margin="5,0"&gt;
      &lt;DataGrid Name="AccountsDataGrid"
                ItemsSource="{Binding Accounts}"
                SelectedItem="{Binding SelectedAccount}"
                AutoGenerateColumns="False"
                CanUserResizeColumns="True"
                SelectionMode="Single"
                SelectionUnit="FullRow"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="ClinicId" 
                                  TextBlock.TextAlignment="Center"
                                  Width="75"
                                  Binding="{Binding ClinicId}" /&gt;
              &lt;DataGridTextColumn Header="Account#"
                                  Width="75"
                                  Binding="{Binding AccountNumber}"/&gt;
              &lt;DataGridTextColumn Header="LastName"
                                  Width="1*"
                                  Binding="{Binding LastName}" /&gt;
              &lt;DataGridTextColumn Header="FirstName"
                                  Width="1*"
                                  Binding="{Binding FirstName}" /&gt;
              &lt;DataGridTextColumn Header="Balance"
                                  Width="Auto"
                                  Binding="{Binding Balance}" /&gt;
              &lt;DataGridTextColumn Header="Follow Up"
                                  Width="100"
                                  Binding="{Binding FollowUpDate}"/&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/ScrollViewer&gt;
  </code></pre>
  
  <p>And the intial Load method in the ViewModel, which is where I want to set the highlighted row.</p>
  
  <pre><code>public void Load()
  {
      RefreshGrid();
      SelectedAccount = Accounts.First();
      _accountId = SelectedAccount.Id;
  }
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>The issue was subtle, but makes perfect sense now.</p>
  
  <pre><code>private Account _selectedAccount;
  
  public Account SelectedAccount
  {
      get =&gt; _selectedAccount;
      set =&gt; SetSelectedAccount(value);
  }
  
  private void SetSelectedAccount(Account value)
  {
      _selectedAccount = value;
      OnPropertyChanged("_selectedAccount");  // &lt;= whoops
      if (_selectedAccount != null)
          OnAccountSelected(
             _selectedAccount.PrimaryKeyFields);
  }
  </code></pre>
  
  <p>Raising this event for a private property doesn't make sense, as the view cannot see it, and is bound to SelectedAccount. Changing it to OnPropertyChanged("SelectedAccount") did the trick.</p>