Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
195
Entering edit mode when typing
posted

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?
Parents Reply Children
No Data