DataGridCell not receiving keyboard focus when set into Edit mode

Apr 12, 2010 at 1:00 PM
Edited Apr 12, 2010 at 1:03 PM

I have a datagrid that is rendered via a DataTemplate for every node in a Graph. Also there is a custom Template Selector that selects the cell editing template based on the properties of the underlying item/object.

Now I wish to programatically set the focus to a particular cell and put it into edit mode ; so that the user can start changing the name of the node. However what I see is that a bounding box appears at the correct location ; if I start typing in characters - the value is not edited and focus moves onto another control (a search box in my case). If I use my mouse to click in the bounding box once, I can use the keyboard to edit the value. The caret appears in the bounding box only once I've clicked in it with the mouse.

A similar thread:

My Code:    (the FindChild is something I cooked up which walks the visual/logical trees.. works perfectly. The GetCell and GetRow are helper methods from the WPF DataGrid Sample code. I'm using DataGrid v3.5.40128.1)  Both the Focus and BeginEdit methods are returning true.    

var dataGrid = node.FindChild<DataGrid>();
var nameItem = dataGrid.Items.OfType<IPropertyDataModel>()
.Where(prop => prop.ID == PropertyTypes.NAME)
var cell = dataGrid.GetCell(dataGrid.Items.IndexOf(browseNameItem), 2);

//var editBox = cell.FindChild<TextBox>();

Next I tried to find the textbox that would be generated as part of the custom editing template,but in vain. The template expansion hadn't taken place at this point of time..
However I find that the SelectTemplate() method is called twice. The first time as soon as I call BeginEdit() -- however at this time, the item parameter of the method is passed in as null ; as a result a null Template is returned from the function. After the function in which I call BeginEdit exits, SelectTemplate is called again ; this time with the right IPropertyDataModel object as the item param and the right DataTemplate is returned. Should this function be called twice ? If yes, what is the point at which I can safely assume that the editing template expansion has taken place ? It seems that a request for expansion is pushed into a message-queue of some sorts which is executed only after the current method exits..

Seems like a basic requirement for the grid... What am I missing here ? The above behavior is also seen in normal edit (not via code). As in I can't enter values into the data grid cells using just the keyboard, I need to use the mouse to click once inside the edit box. The caret appears and then I can use my keyboard to enter text..

Any ideas are welcome... I've been at this for hours now.