This older post has been a lifesaver. I have been on this problem for past few hours and dkozl provided some great insight. Hopefully my explanation will help a few others.
I have a datagrid with some columns (7 to be exact) 5 of them display static data on the item, 1 is a textbox entry/data display, and the last one is an indicator for on and off for boolean items.
I am currently improving the user experience while using the datagrid (using F2 to edit, space to toggle boolean values, and so forth)
The XAML below i will only have one column in datagrid with a textbox:
<DataGrid x:Name="my_DG" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" KeyDown="my_DG_KeyDown">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<TextBlock Text="Value" FontSize="18"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtBoxValue" Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" KeyDown="EventTrigger_KeyDown"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsTabStop" Value="False" />
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
<DataGrid.Columns>
</DataGrid>
The effect i was going for is when i had a row highlighted on the datagrid when i push F2 focus would go to the textbox and the user can start editing the value. This was done with the KeyDown="my_DG_KeyDown" event. The code on the event is:
private void my_DG_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.F2))
{
var currRow = (sender as DataGrid).CurrentItem;
//Columns[0] is the column the text box is in for the given row.
var currCell_CP = (sender as DataGrid).Columns[0].GetCellContent(currRow);
var itm = (currCell_CP as ContentPresenter).ContentTemplate.FindName("txtBoxValue", currCell_CP) as TextBox;
itm.Focus();
}
}
The important take away is i was able to get the ContentPresenter of a given Cell. Then from there i was able to get the template and search for the textbox name ("txtBoxValue") with those 2 items.
I find this a bit more straight forward than dkozl answer, but i wouldn't have came to this without his help