I'm trying to make the grid enter edit mode when typing and I *almost* have it working. I've read some other posts where you say it isn't supported because of the lack of a preview event for the key down, but I think I might be close enough to get it working here with some help.
The basic idea is to remember the key pressed and then manually enter edit mode and put the key they pressed back in the box before they type the next character. Here is what I have:
private void dataGrid_KeyDown(object sender, KeyEventArgs e) { //not quite working yet if (!isEditing && !IsNavigationKey(e) && dataGrid.ActiveCell.IsEditable) { bool isShifty = ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift); string letter = ((char)e.PlatformKeyCode).ToString(); letter = (isShifty ? letter.ToUpper() : letter.ToLower()); dataGrid.ActiveCell.Tag = letter; dataGrid.EnterEditMode(dataGrid.ActiveCell); } }
private static bool IsNavigationKey(KeyEventArgs e) { return new[] { Key.Delete, Key.Escape, Key.Enter, Key.Down, Key.Up, Key.Left, Key.Right, Key.Tab, Key.Shift, Key.Ctrl, Key.Alt }.Contains(e.Key); } private bool isEditing = false; private void dataGrid_CellEnteredEditMode(object sender, Infragistics.Controls.Grids.EditingCellEventArgs e) { isEditing = true; var textBox = e.Editor as TextBox; if (e.Cell.Tag != null && textBox != null) { textBox.Text = e.Cell.Tag.ToString(); e.Cell.Tag = null; //this is the only way I could keep it from having the text highlighted for editing //which makes the first character get deleted when the next one is typed System.Threading.ThreadPool.QueueUserWorkItem(x => { System.Threading.Thread.Sleep(50); Dispatcher.BeginInvoke(() => { textBox.Focus(); textBox.Select(1, 0); textBox.Focus(); }); }); } } This pretty much works except the row isn't exiting edit mode when I hit enter when done editing. Any idea why?
private static bool IsNavigationKey(KeyEventArgs e) { return new[] { Key.Delete, Key.Escape, Key.Enter, Key.Down, Key.Up, Key.Left, Key.Right, Key.Tab, Key.Shift, Key.Ctrl, Key.Alt }.Contains(e.Key); } private bool isEditing = false; private void dataGrid_CellEnteredEditMode(object sender, Infragistics.Controls.Grids.EditingCellEventArgs e) { isEditing = true; var textBox = e.Editor as TextBox; if (e.Cell.Tag != null && textBox != null) { textBox.Text = e.Cell.Tag.ToString(); e.Cell.Tag = null; //this is the only way I could keep it from having the text highlighted for editing //which makes the first character get deleted when the next one is typed System.Threading.ThreadPool.QueueUserWorkItem(x => { System.Threading.Thread.Sleep(50); Dispatcher.BeginInvoke(() => { textBox.Focus(); textBox.Select(1, 0); textBox.Focus(); }); }); } }
This pretty much works except the row isn't exiting edit mode when I hit enter when done editing. Any idea why?
Ping
Hi,
I tried out your code, and it appears to work fine. WIth the exception that i had to add this:
void grid_CellExitedEditMode(object sender, CellExitedEditingEventArgs e)
{
isEditing = false;
}
Hitting enter exited edit mode for me no problem.
Now, i don't have any editing settings turned on, and i tried this on a TextColumn. If you have a custom editor or any editor that handles the enter key, then the xamGrid won't exit edit mode, as it defaults to the editor's key handling first.
-SteveZ
Argh I was so focused on the grid side forgot to look at that - Thanks for fixing my bug!
Actually I guess I spoke too soon.. I did have that in there and just didn't post that method. It doesn't seem to make a difference either way though as that code doesn't affect if the grid adds the new row or not. I am using textcolumns. When I hit enter after typing the cellexited event fires but not the keydown or the rowedit ended. If I enter the cell by double clicking or hitting F2/Enter those events do fire and the row does get added successfully.
Ah, you want the row to enter edit mode.
Then do this:
grid.EnterEditMode((Row)grid.ActiveCell.Row, grid.ActiveCell);