title: WPF DataGrid disable cell edit based on value
body: <p>I have a WPF <code>DataGrid</code> populated with an <code>ObservableCollection&lt;ZeroConnection&gt;</code> named <code>ZeroTable</code>. If <code>ZeroTable.NetID == 0</code> the <code>DataGrid</code> row should be disabled and that works. Furthermore if <code>ZeroTable.Number == ""</code> editing of that particular cell should be disabled. But have not succeeded to implement this as the user still can edit the cell. My XAML below;</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;local2:RowReadOnlyConverter x:Key="RowReadOnlyConverter" /&gt;
      &lt;local2:CellReadOnlyConverter x:Key="CellReadOnlyConverter" /&gt;
  &lt;/Window.Resources&gt;
  
  &lt;DataGrid Name="ZeroTableGridView" Grid.Column="0"  AlternatingRowBackground="#FFEEEEEE" AutoGenerateColumns="False" 
            ItemsSource="{Binding ZeroTable}"  CanUserAddRows="False"
            dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"  dd:DragDrop.DropTargetAdornerBrush="Coral" 
            dd:DragDrop.DropHandler="{Binding }"&gt;
      &lt;DataGrid.Resources&gt;
          &lt;Style TargetType="{x:Type DataGridRow}"&gt;
              &lt;Style.Triggers&gt;
                  &lt;DataTrigger Binding="{Binding NetID, Converter={StaticResource RowReadOnlyConverter}}" Value="True"&gt;
                      &lt;Setter Property="IsEnabled" Value="False" /&gt;
                  &lt;/DataTrigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/DataGrid.Resources&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Nummer" Binding="{Binding Number}" CanUserSort="False" IsReadOnly="False"&gt;
              &lt;DataGridTextColumn.ElementStyle&gt;
                  &lt;Style TargetType="{x:Type TextBlock}"&gt;
                      &lt;Style.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding Number, Converter={StaticResource CellReadOnlyConverter}}" Value="True"&gt;
                              &lt;Setter Property="IsEnabled" Value="False" /&gt;
                              &lt;Setter Property="Background" Value="HotPink"/&gt; &lt;!-- For debugging --&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/DataGridTextColumn.ElementStyle&gt;
          &lt;/DataGridTextColumn&gt;
          &lt;DataGridTextColumn Header="Ledning" Binding="{Binding Wire}" CanUserSort="False" IsReadOnly="False"/&gt;
          &lt;DataGridTextColumn Header="Postbeteckning" Binding="{Binding ItemDes}" CanUserSort="False" IsReadOnly="True"/&gt;
          &lt;DataGridTextColumn Header="Uttag" Binding="{Binding Terminal}" CanUserSort="False" IsReadOnly="True"/&gt;
          &lt;DataGridTextColumn Header="Hänvisning" Binding="{Binding Ref}" CanUserSort="False" IsReadOnly="True"/&gt;
          &lt;DataGridTextColumn Header="Kretsschema" Binding="{Binding Sheet}" CanUserSort="False" IsReadOnly="False"/&gt;
          &lt;DataGridTextColumn Header="Anmärkning" Binding="{Binding Remark}" CanUserSort="False" IsReadOnly="False"/&gt;
          &lt;!--&lt;DataGridTextColumn Header="ID" Binding="{Binding NetID}" CanUserSort="False"/&gt; --&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>And the C# code below;</p>
  
  <pre><code>public class CellReadOnlyConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          string number = (string)value;
          if (number == "")
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new Exception("The method or operation is not implemented.");
      }
  }
  
  public class RowReadOnlyConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          int netID = (int) value;
          if (netID == 0)
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new Exception("The method or operation is not implemented.");
      }
  }
  </code></pre>
  
  <p>The cell "number" that contains an empty string is still editable. The background of the cell does change so the <code>DataTrigger</code> seems to be activated. Help is appreciated</p>