I am facing a strange problem with the WPF DataGrid. If you click the first cell, enter a value and then tab to the next cell, the first cell does not exit editing mode. I have reproduced the problem using a simplified version of the templates below:
<DataGrid Name="grid" HorizontalAlignment="Stretch" ItemsSource="{Binding Persons}" Margin="0,10,0,0" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="FirstName">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding FirstName}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="LastName">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding LastName}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Age">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Age}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Age}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The dummy class definition is:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
The code behind is:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Persons = new ObservableCollection<Person>();
this.grid.CurrentCellChanged += grid_CurrentCellChanged;
this.grid.PreparingCellForEdit += grid_PreparingCellForEdit;
}
void grid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
if (e.EditingElement != null)
SetFocusToTextBox(e.EditingElement);
}
void grid_CurrentCellChanged(object sender, EventArgs e)
{
((DataGrid)sender).BeginEdit();
}
public ObservableCollection<Person> Persons { get; set; }
void SetFocusToTextBox(object obj)
{
// Get all children and examine if the child is a TextBox
object obChild;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj as DependencyObject); i++)
{
obChild = VisualTreeHelper.GetChild(obj as DependencyObject, i);
if (obChild is TextBox)
{
((TextBox)obChild).Focus();
break;
}
else
SetFocusToTextBox(obChild);
}
}
Does anyone see what's wrong here? Are you able to reproduce the issue? Any help will be appreciated.
Thanks, Bhanu