title: WPF datagrid binding issue: first row bound but doesn't update selected item in two-way fashion
body: <h2>The Setup</h2>
  
  <ul>
  <li>I have a DataGrid that holds a list of custom types ("Previous Documents") and binds to them. That part works correctly.</li>
  <li>I have bound the <code>SelectedItemProperty</code> of the datagrid to a property on my ViewModel called <code>CurrentlySelectedPreviousDocument</code></li>
  <li>I have the left-click action of the datagrid bound to a command called <code>OpenPreviousDocumentCommand</code></li>
  <li>The <code>OpenPreviousDocumentCommand</code> runs a method called <code>OpenSelectedPreviousDocument()</code></li>
  <li><code>OpenSelectedPreviousDocument()</code> uses the <code>CurrentlySelectedPreviousDocument</code> property to copy a file to the temporary directory and open it.</li>
  </ul>
  
  <h2>The Problem</h2>
  
  <ul>
  <li>This code works, but only if you click on the first item in the datagrid. </li>
  <li>For every other item in the datagrid, it appears that CurrentlySelectedPreviousDocument is shown as null, so all of the properties are null.</li>
  </ul>
  
  <p>More scenarios to help describe the problem:</p>
  
  <ul>
  <li>When I load the application, no rows are selected.</li>
  <li>If I double-click the first item at any point, it opens, even after clicking other items and seeing errors.</li>
  <li>If I double-click the second or third items first, I receive the error.</li>
  <li>If I double-click the first document to open it, then single-click another row to ensure it is selected, and then double-click on the other row, I still receive the error.</li>
  <li>If I double-click the first document to open it, then click one of the faulty rows and receive an error, and then click on the first row again, it still works.</li>
  </ul>
  
  <h2>Things I've Tried So Far:</h2>
  
  <ul>
  <li>Updating the bingding mode to TwoWay</li>
  <li>Adding an UpdateSourceTrigger = PropertyChanged</li>
  <li>Checked the output window; no invalid bindings detected.</li>
  </ul>
  
  <h2>The Code</h2>
  
  <p><strong>XAML DataGrid bindings:</strong></p>
  
  <pre><code>&lt;DataGrid 
      x:Name="PreviousDocumentsDataGrid"
      ItemsSource="{Binding PreviousDocumentsList}" 
      SelectedItem="{Binding CurrentlySelectedPreviousDocument, Mode=OneWayToSource}" 
      SelectionMode="Single"
      SelectionUnit="FullRow" 
      AutoGenerateColumns="False" 
      IsReadOnly="True" 
      HorizontalGridLinesBrush="LightGray" 
      VerticalGridLinesBrush="LightGray" 
      BorderBrush="Transparent" 
      Visibility="{Binding PreviousDocumentsFound, Converter={StaticResource BoolToVisConverter}}"&gt;
      &lt;DataGrid.InputBindings&gt;
          &lt;MouseBinding Gesture="LeftDoubleClick" Command="{Binding OpenPreviousDocumentCommand}"/&gt; 
      &lt;/DataGrid.InputBindings&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Name" Binding="{Binding Name}"/&gt;
          &lt;DataGridTextColumn Header="Reference Type" Binding="{Binding ReferenceType}"/&gt;
          &lt;DataGridTextColumn Header="Category ID" Binding="{Binding Category}"/&gt;
          &lt;DataGridTextColumn Header="Description" Binding="{Binding Description}"/&gt;
          &lt;DataGridTextColumn Header="Document Timestamp" Binding="{Binding Timestamp}"/&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>The ViewModel definition of CurrentlySelectedPreviousDocument:</strong></p>
  
  <pre><code>public VEDocument CurrentlySelectedPreviousDocument
  {
      get { return _currentlySelectedPreviousDocument; }
      set { _currentlySelectedPreviousDocument = value;
      OnPropertyChanged("CurrentlySelectedPreviousDocument");} //TODO: Is the on propertychanged actually necessary here?
  }
  </code></pre>
  
  <p><strong>Command Definition:</strong></p>
  
  <pre><code>public ICommand OpenPreviousDocumentCommand
  {
      get
      {
          return _openPreviousDocumentCommand ??
                 (new CommandHandler(OpenSelectedPreviousDocument, _canExecuteCommands));
      }
  }
  </code></pre>
  
  <p><strong>Method in the ViewModel to open the document (uses the viewmodel property)</strong></p>
  
  <pre><code>public void OpenSelectedPreviousDocument()
  {
      var docToOpen = CurrentlySelectedPreviousDocument;
      ...etc. etc.
  }
  </code></pre>