title: How do I prevent a DataGridTemplateColumn from going into edit-mode based on some condition?
body: <p>I have a DataGridTemplateColumn that is editable.  I only want the user to be able to edit the content of a cell in this column if the business object satisfies some criterion.  Suppose my business object implements INotifyPropertyChanged and has three properties: <em>Name</em>, <em>Department</em>, and <em>Sales</em>.  <em>Name</em> and <em>Department</em> are strings, and <em>Sales</em> is a double.</p>
  
  <p>I want the user to be able to edit the <em>Sales</em> value only if <em>Department</em> equals "Retail".  Here's a datagrid I might use to do this:</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding Path=MyTypeCollection}" AutoGenerateColumns="False"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Name" Binding="{Binding Path=Name, Mode=OneTime}" IsReadOnly="True" /&gt;
          &lt;DataGridTextColumn Header="Department" Binding="{Binding Path=Department, Mode=OneTime}" IsReadOnly="True" /&gt;
          &lt;DataGridTemplateColumn Header="Sales"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock Text="{Binding Path=Sales, Mode=TwoWay}" Visibility="{Binding Path={StaticResource ResourceKey=IsRetail}}" /&gt;
                          &lt;TextBlock Text="{Binding Path=Sales, Mode=OneWay}" Visibility="{Binding Path={StaticResource ResourceKey=IsNotRetail}}" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBox Text="{Binding Path=Sales, Mode=TwoWay}" Visibility="{Binding Path={StaticResource ResourceKey=IsRetail}}" /&gt;
                          &lt;TextBlock Text="{Binding Path=Sales, Mode=OneWay}" Visibility="{Binding Path={StaticResource ResourceKey=IsNotRetail}}" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I'm using a stack panel with two Text elements.  I use a binding in the Visibility of the Text elements to toggle Text elements.  If I have a row with a <em>Department</em> value that is not "Retail", I display a TextBlock in the <em>Sales</em> column whether the cell is in display mode or edit mode.</p>
  
  <p>This seems like a clumsy solution to me.  Is there some way I can prevent these types of cells from entering edit mode entirely?  I only want to allow edit mode in the case that the <em>Department</em> is "Retail".  Is this possible?</p>
  
  <p>Edit: Adding code.</p>
  
  <p>@Rachel.  Thank you for your help.  I want to paste in all of my datagrid XAML code to make sure I have everything right.  </p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Data}"&gt;
  
      &lt;DataGrid.Resources&gt;
          &lt;DataTemplate x:Key="TextBoxTemplate"&gt;
              &lt;TextBox Text="{Binding Path=Sales}" /&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGrid.Resources&gt;
  
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Department" Binding="{Binding Path=Department, Mode=OneTime}" /&gt;
          &lt;DataGridTextColumn Header="Name" Binding="{Binding Path=Name, Mode=OneTime}" /&gt;
  
          &lt;DataGridTemplateColumn&gt;
              &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ContentControl x:Name="salesControl"&gt;
                          &lt;TextBlock Text="{Binding Sales}" /&gt;
                      &lt;/ContentControl&gt;
  
                      &lt;DataTemplate.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding Department}" Value="Retail"&gt;
                              &lt;Setter TargetName="salesControl" Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" /&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/DataTemplate.Triggers&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
  
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBlock Text="{Binding Sales}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
  
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I've almost got this where I want it.  When I click on a <code>Sales</code> value in a <code>Retail</code> row, a TextBox appears but it doesn't have a value in it (see <a href="http://i.imgur.com/92u3Tw9.png" rel="nofollow">here</a>).  I'm not sure why it doesn't have a value because the TextBox in the TextBoxTemplate specifies a binding.  Do you know why this is?</p>
  
  <p>Edit: I noticed one other problem with this solution, I can't actually edit the value in the Sales column.  If I try, the value reverts to the original pre-edit value.</p>