title: Stop the cell to go into edit mode when it is double clicked
body: <p>I have a DataGrid. I have Edit Button on each row of my DataGrid. When user clicks on this edit button every cell of that row goes into edit mode. When I click on any edit button it's template changes to save button. When I am done I click on that save button and DataGridRow comes out of the edit mode and Save Button again changes to Edit Button. This works fine.</p>
  
  <p>Here is my code:</p>
  
  <p>Xaml:</p>
  
  <pre><code>&lt;DataGrid Grid.Column="1" Grid.Row="3"  AutoGenerateColumns="False" RowHeaderWidth="0" GridLinesVisibility="Vertical"
            ItemsSource="{Binding Source={StaticResource ItemsViewSource}}" SelectedItem="{Binding SelectedItem}"
            CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False"
            SelectionMode="Single" SelectionUnit="FullRow"&gt;
  
      &lt;DataGrid.Columns&gt;
  
          &lt;DataGridTemplateColumn Header="Name of the Item" Width="*" SortDirection="Ascending"&gt;
              &lt;DataGridTemplateColumn.HeaderStyle&gt;
                  &lt;Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}"&gt;
                      &lt;Setter Property="HorizontalContentAlignment" Value="Left"/&gt;
                  &lt;/Style&gt;
              &lt;/DataGridTemplateColumn.HeaderStyle&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBlock Text="{Binding Name}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" PreviewKeyDown="TextBox_PreviewKeyDown"/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
              &lt;DataGridTemplateColumn.CellStyle&gt;
                  &lt;Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}"&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt;
                                  &lt;Grid Background="{TemplateBinding Background}"&gt;
                                      &lt;ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0,0,0" /&gt;
                                  &lt;/Grid&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/DataGridTemplateColumn.CellStyle&gt;
          &lt;/DataGridTemplateColumn&gt;
  
          &lt;DataGridTemplateColumn Header="Edit" Width="50" IsReadOnly="True"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Button Content="Edit" Style="{StaticResource EditSaveButton}"  
                              Command="{Binding DataContext.EditItemCommand, 
                                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}" 
                              Click="EditButton_Click"/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
  
          &lt;DataGridTemplateColumn Header="Delete" Width="70" IsReadOnly="True"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Button Content="Delete" Style="{StaticResource DeleteButton}"
                              Command="{Binding DataContext.CancelItemEditOrDeleteCommand, 
                                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page}}"/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
  
      &lt;/DataGrid.Columns&gt;
  
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>code-behind(cs):</p>
  
  <pre><code>void EditButton_Click(object sender, RoutedEventArgs e)
  {
      int colIndex = 0;
      int rowIndex = 0;
  
      DependencyObject dep = (DependencyObject)e.OriginalSource;
      while (dep != null &amp;&amp; !(dep is DataGridCell))
      {
          dep = VisualTreeHelper.GetParent(dep);
      }
  
      if (dep == null)
          return;
      DataGridRow row = null;
      if (dep is DataGridCell)
      {
  
          colIndex = ((DataGridCell)dep).Column.DisplayIndex;
  
          while (dep != null &amp;&amp; !(dep is DataGridRow))
          {
              dep = VisualTreeHelper.GetParent(dep);
          }
  
          row = (DataGridRow)dep;
          rowIndex = FindRowIndex(row);
  
      }
  
      while (dep != null &amp;&amp; !(dep is DataGrid))
      {
          dep = VisualTreeHelper.GetParent(dep);
      }
  
      if (dep == null)
          return;
  
      DataGrid dg = (DataGrid)dep;
      if (row != null)
      {
          var rows = GetDataGridRows(dg);
  
          if (row.IsEditing)
          {
              dg.CommitEdit(DataGridEditingUnit.Row, true);
  
              foreach (DataGridRow r in rows)
              {
                  r.IsEnabled = true;
              }
          }
          else
          {
              dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[0]);
              dg.BeginEdit();
  
              foreach (DataGridRow r in rows)
              {
                  if (!(r.IsEditing))
                  {
                      r.IsEnabled = false;
                  }
              }
          }
      }
  }
  
  public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
  {
      var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
      if (cellContent != null)
          return (DataGridCell)cellContent.Parent;
  
      return null;
  }
  
  private int FindRowIndex(DataGridRow row)
  {
      DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid;
  
      int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row);
  
      return index;
  }
  
  public IEnumerable&lt;DataGridRow&gt; GetDataGridRows(DataGrid grid)
  {
      var itemsSource = grid.ItemsSource as IEnumerable;
      if (null == itemsSource) yield return null;
      foreach (var item in itemsSource)
      {
          var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
          if (null != row) yield return row;
      }
  }
  </code></pre>
  
  <p>Basically I have 2 questions here:</p>
  
  <ol>
  <li>When I DoubleClick a cell, that cell goes into edit mode. I want to stop that behavior as I am providing the edit button.</li>
  <li>Once any row goes to edit mode, and if I change data in 1st cell of that row and now if I move to the next cell, the 1st cell changes its template to cellTemplate. I don't want to change its template. I mean I want it to remain in it's cellEditTemplate upto the time when I click on Edit Button again.</li>
  </ol>
  
  <p>If anybody has an answer to any one of the question, then also please post.</p>
  
  <p>Thanks.</p>