I have the need when editing a WPF 4 DataGrid to focus on a named Textbox in a templated column grid when the user presses the down key- this is the same functionality as Excel provides and allows users to quickly mass edit a number of rows.

Working out how to do this was a little bit trickier than I imagined so I’ve decided to blog the answer in case anyone else has the same issue.

In our example, we have a simple datagrid with 1 templated column containing a vanilla Textbox as shown below:

The clever bit is the FindbyName method, which I found on StackOverflow in this answer. When the down key is pressed, the associated collection with the bound data is found, the collection view is moved to the next item, and because the datagrid has its IsSynchronizedWithCurrentItem property set to true, the next row in the grid is highlighted.

It is then *simply* a case of getting the selected item in the grid, getting the container from the item and then using the FindByName method, which interestingly uses an explicitly stack, as opposed to using the stack provided by writing a recursive function.

Once the named control is found it is simply a case of calling focus on the control. The FindByName bit really is the non obvious part. Hopefully this post will help someone else out, especially if like me you initially try using FrameworkElement.FindNameand find it does not work!