I am working on a WPF MVVM application that uses DataGrids
to display data. I am manually defining the columns, and most of the time I use DataGridTextColumn
.
When navigating the resulting datagrid using the keyboard, a cell can be highlighted by navigating to it, and the cell value can be changed by starting to type a value. This behaviour works for DataGridTextColumn
.
In some cases, I have to use DataGridTemplateColumn
, with different bindings for the CellTemplate
and the CellEditingTemplate
. This allows me to format the displayed value, but return to an unformatted value once the user starts editing the cell. However, the keyboard navigation and editing method described above does not work for my current implementation. I have to highlight the cell and press F2 to edit, or I have to click on the cell with the mouse.
Below is a mockup of my current implementation, in which Foo
is in a DataGridTextColumn
which works fine, and Bar
is in a DataGridTemplateColumn
which is giving me trouble.
How can I change the definition of my DataGridTemplateColumn
so it has the same keyboard navigation and editing functionality as the DataGridTextColumn
?
<DataGrid ItemsSource="{Binding ListItems}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Foo}"
Header="Foo" />
<DataGridTemplateColumn Header="Bar">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Path=BarString, Mode=OneWay}"
HorizontalAlignment="Right" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding BarValue, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
HorizontalContentAlignment="Right" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>