title: WPF DataGrid - How to cancel edit row after validation error?
body: <p>I want to cancel changes in a row when user click ✖ button.</p>
  
  <p><a href="https://i.stack.imgur.com/xsLO0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xsLO0.png" alt="enter image description here"></a> <a href="https://gist.github.com/marbel82/38435f3cd8a8a3b9e373a1e62d901f2b" rel="nofollow noreferrer">source code</a></p>
  
  <pre class="lang-cs prettyprint-override"><code>private void CancelChangesButton_Click(object sender, RoutedEventArgs e)
  {
      datagrid.CancelEdit();
  }
  </code></pre>
  
  <p><strong>CancelEdit() works great, until... my DateConverter can't ConvertBack a string.</strong> The same behaviour occurs when property setter of a ViewModel throws an exception.
  I can't do anything in DataGrid. The only way is to press the ESC key when cursor is in the red cell.</p>
  
  <p>I try other things:</p>
  
  <pre class="lang-cs prettyprint-override"><code>datagrid.CancelEdit(DataGridEditingUnit.Row);
  datagrid.CancelEdit(DataGridEditingUnit.Cell);
  datagrid.CommitEdit();
  datagrid.IsReadOnly = true;
  // Add new item
  </code></pre>
  
  <p>Nothing happened.</p>
  
  <p>So I started to dig in the .NET Framework sources and I found this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class DataGrid : MultiSelector
  ...
      public bool CancelEdit(DataGridEditingUnit editingUnit)
      {
          return EndEdit(CancelEditCommand, CurrentCellContainer, editingUnit, true);
      }
  </code></pre>
  
  <p><a href="https://referencesource.microsoft.com/PresentationFramework/R/5cb932042ceb6d8c.html" rel="nofollow noreferrer">-> .NET Reference source</a></p>
  
  <p>The most important thing here is CurrentCellContainer that gets the value from CurrentCell. Next, I discovered that CurrentCell is following the focus. When I click ✖ button, CurrentCell changes to cell in Action column, and when I click outside DataGrid, CurrentCell changes to null.</p>
  
  <p>So, I have to change CurrentCell to cell with validation error, and than invoke CancelEdit().
  Do I think right?</p>
  
  <p><strong>How to find all cells with a validation error?</strong></p>
  
  <p>Is there another way to cancel editing?</p>