I am using Microsoft WPF datagrid. I have noticed a strange behavior with WPF datagrid DataGridTemplateColumn. When you use the templateColumn in the grid and the template column contains some controls when you tab from the previous column the focus is not automatically given to the first element declared in the template column. The foucs is initally set on the border of the template column and when we tab of once agin the focus goes to the first column. Any workaround for this issue. How can i set the focus to go the first element in the template column of the datagrid when i tab off.
Asked
Active
Viewed 1.1k times
23
-
Some other answers can be found on this question: http://stackoverflow.com/questions/1104164/wpf-datagridtemplatecolumn-am-i-missing-something – Neil Nov 18 '11 at 14:23
4 Answers
12
We solved this problem by modifying the style on DataGridCell:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="IsTabStop" Value="False"/>

patrickbadley
- 2,510
- 2
- 29
- 30
7
I got rid of this problem by handling PrepareCellForEdit event of the grid. Here is the code
void HODataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
UIElement inputElement;
///
/// Texbox is the first control in my template column
///
inputElement = HODataGridHelper.GetVisualChild<TextBox>(e.EditingElement);
if (inputElement != null)
{
Keyboard.Focus(inputElement);
}
}

Gopinath
- 1,858
- 7
- 31
- 50
-
What is the HODataGridHelper? I tried using this code in a .NET 4 project, but it has no idea what the HODataGridHelper is, and I can't find an applicable GetVisualChild function anywhere else. – Pete Aug 10 '11 at 11:55
-
Pete, that was a custom class we created. Sorry I don't access to that piece of code now. It's a variant of framework's GetVisualChild method : http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getvisualchild.aspx – Gopinath Aug 14 '11 at 06:19
-
3
There is a solution using a static class and one change to the Xaml for the control you want focused. "WPF DataGrid: Tabbing from cell to cell does not set focus on control"

the Tin Man
- 158,662
- 42
- 215
- 303

amaca
- 1,470
- 13
- 18
-
-
1This works: http://iyalovoi.wordpress.com/2009/08/21/wpf-datagrid-tabbing-from-cell-to-cell-does-not-set-focus-on-control/ – amaca Oct 26 '11 at 18:12
1
I found out a link in WPF datagrid codeplex discussions http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=35540
Thanks to vincent sibal