title: Validate row when underlying data changes
body: <p>Imagine a DataGrid with its <code>ItemsSource</code> set to an <code>ObservableCollection</code>. This collection provides a view model for each row in the <code>DataGrid</code>. The view model in turn provides the data that is displayed in one row and a command that may change this data. Additionally, I added a rule to the  <code>RowValidationRules</code> property of <code>DataGrid</code>. This validation rule works fine in case I enter invalid data.</p>
  
  <p>However, if I change the invalid data to valid data via the command the view model provides, the row validation rule only gets triggered again if the current row in <code>DataGrid</code> loses focus. Hence, the displayed data may be actually valid, but the <code>DataGrid</code> still displays a red exclamation mark showing it has invalid data. This remains the case until the current row loses focus or I enter valid data again.</p>
  
  <p>How do I force a second validation of the current row? I already set <code>ValidatesOnTargetUpdated="True"</code> but this didn't solve the problem. I also have implemented the INotifyPropertyChanged interface but this also didn't fix the problem.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>As user <a href="https://stackoverflow.com/a/42484903/1549233">mm8</a> pointed out, <code>INotifyDataErrorInfo</code> is the approach to go. I removed the row validation rule and exposed a property named <code>HasErros</code> in my view model that proxies the <code>HasErrors</code> property of my model that in turn implements <code>INotifyDataErrorInfo</code>. Next I added a custom <code>RowValidationErrorTemplate</code></p>
  
  <pre><code>&lt;DataGrid.RowValidationErrorTemplate&gt;
      &lt;ControlTemplate&gt;
          &lt;Grid&gt;
              &lt;Ellipse Width="12" Height="12" Fill="Red"/&gt;
              &lt;Label Content="!" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                     Foreground="White" FontSize="11"/&gt;
          &lt;/Grid&gt;
      &lt;/ControlTemplate&gt;
  &lt;/DataGrid.RowValidationErrorTemplate&gt;
  </code></pre>
  
  <p>and created the following custom style for <code>DataGridRowHeader</code></p>
  
  <pre><code>&lt;Style x:Key="MyDataGridRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}"&gt;
      &lt;!-- ... --&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridRowHeader}"&gt;
                  &lt;Border&gt;
                      &lt;Grid&gt;
                          &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /&gt;
                          &lt;Control SnapsToDevicePixels="True"
                                   Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"
                                   Visibility="{Binding Path=HasErrors, UpdateSourceTrigger=PropertyChanged, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"/&gt;
                      &lt;/Grid&gt;
                  &lt;/Border&gt;
                  &lt;!-- ... --&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Note the binding of <code>Visibility</code>. The <code>HasErrors</code> property is the proxy property I mentioned above.</p>
  
  <p>And finally, use that style in the <code>DataGrid</code> as follows</p>
  
  <pre><code>&lt;DataGrid RowHeaderStyle="{StaticResource MyDataGridRowHeaderStyle}"
  ...
  </code></pre>
  
  <p>An implementation of <code>BoolToVisibilityConverter</code> can be found <a href="https://stackoverflow.com/a/28517042/1549233">here</a>.</p>