-
WPF Datagrid and the dreaded double-tab focus issue
Posted on November 24th, 2009 5 commentsSo I just had to figure this out on my own over the last hour or so and found it to be a good candidate for a post. I found my path through the internet on this one but apparently everyone’s solution didn’t exactly equal to mine for some reason or another. So here we go:
Problem:
When using a DataGridTemplateColumn from the DataGrid in the WPF Toolkit in .NET 3.5 (after attending PDC09, I know .NET4 will have a built-in WPF DataGrid), if you want to simply use tab to change the focus between controls within the cells, you’ll have to double-tab to move to the next control.
For example, if I have a textbox in a cell and want to move to a textbox in another cell within the datagrid with TAB, hitting TAB once will move me to the next DataGridCell… then I have to press TAB again to move to the TextBox inside that DataGridCell.
Solution:
After googling around, I frequently hit this site which supposedly fixed this problem with a nice little solution: http://blog.yalovoi.net/2009/08/21/
For some reason unknown to me and not worth my time to investigate, it doesn’t work at all. Here’s what I came up with which is uglier but, in my case, works. I simply navigate the tree from the DataGrid to the textbox.
Forgive my quick and dirty coding but it gets the job done… I’m sure you could easily spawn a much more generic code to do this:
private void uxFieldMappingsDataGrid_CurrentCellChanged(object sender, EventArgs e) { uxFieldMappingsDataGrid.BeginEdit(); } private void uxFieldMappingsDataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e) { ContentPresenter cp = (ContentPresenter)e.EditingElement; TextBox destinationTextBox = VisualTreeHelper.GetChild(cp, 0) as TextBox; if (destinationTextBox != null && destinationTextBox.Name == "uxTest") { destinationTextBox.Focus(); } }You can see where I climb the visual tree down to the text box. Very simple code for a very specific reason that I’m sure you can mold to make your own if you find that you are having the same problems as I am (and using code-behind to solve it isn’t against your religion).
5 responses to “WPF Datagrid and the dreaded double-tab focus issue”

-
The guidance you are providing here is excellent and I was able to create cutomized solution for me. Thank you so much.
http://pubudini.blogspot.com/2010/09/wpf-data-grid-bound-coumn-for-numeric.html
-
Harry Alm October 28th, 2010 at 03:27
Thank you very much!
After searching the web for hours this is the first sollution that really works for me. -
thanks
-
Just making it a bit more generic, thanks for the solution.
private void dataGridCurrentCellChanged(object sender, EventArgs e)
{
((DataGrid)sender).BeginEdit();
}private void dataGridPreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
ContentPresenter cp = (ContentPresenter)e.EditingElement;UIElement destinationTextBox = VisualTreeHelper.GetChild(cp, 0) as UIElement;
if (destinationTextBox != null)
{
destinationTextBox.Focus();
}
}
-
Very helpful, the first solution to this problem that actually worked. Thanks!
Leave a reply
-


Pubudini Prasanna September 16th, 2010 at 12:07