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?
HI,
Do you need further assistance regarding this forum thead?
Sincerely,
Matt Developer Support Engineer
I am interested in knowing how to do this as well. I have attempted a similar solution based on this post and some others, but the hang up still seems to be getting the first key pressed passed in to the text field.
Hello Gary,
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.
Hi Stefan, I more or less implemented the solution you posted except I used
!((Cell)curvesGrid.ActiveCell).IsEditing
instead of the isEditing flag and CellExitedEditMode callback to determine if the cell was already in edit mode. I am not using Silverlight, so I had to do something else for the conversion from key press to actual character. I am also using a XamCurrencyEditor instead of a textbox, so I had to use:
editor.SelectionStart = editor.TextLength - cursorLocation; editor.SelectionLength = 0;
instead of the textbox Select() method. the only issue I have left is to figure out how to get the XamCurrencyEditor to display a "-" when the user types it (the editor is bound to a decimal in the background).
As far as the subject of this original post goes, your example works, but I was looking for a less clunky version (the dispatched/thread/sleep part to clear the selection is really getting out there).
Thanks!
I have implemented the approach form the previous posts and created a sample project for you, which you can use as basis for any further implementation.