Skip to content

Entering edit mode when typing

New Discussion
infrawtf
infrawtf asked on Aug 8, 2014 8:39 AM

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?
Sign In to post a reply

Replies

  • 0
    infrawtf
    infrawtf answered on Feb 25, 2011 2:14 PM

    Ping

  • 0
    Stephen Zaharuk
    Stephen Zaharuk answered on Feb 25, 2011 4:21 PM

    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

    • 0
      infrawtf
      infrawtf answered on Feb 25, 2011 4:57 PM

      Argh I was so focused on the grid side forgot to look at that – Thanks for fixing my bug!

      • 0
        infrawtf
        infrawtf answered on Feb 25, 2011 5:09 PM

        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.

      • 0
        Stephen Zaharuk
        Stephen Zaharuk answered on Feb 25, 2011 5:49 PM

        Ah, you want the row to enter edit mode. 

        Then do this:

          grid.EnterEditMode((Row)grid.ActiveCell.Row, grid.ActiveCell);

        -SteveZ

      • 0
        infrawtf
        infrawtf answered on Feb 25, 2011 5:57 PM

        Seems like we are going in circles. I'll try and work up a full sample that reproduces the issue by the end of today.

  • 0
    [Infragistics] Matt Traynor
    [Infragistics] Matt Traynor answered on Mar 18, 2011 4:06 PM

    HI,

     Do you need further assistance regarding this forum thead?

     Sincerely,

     Matt
     Developer Support Engineer

     

    • 0
      Gary Wrinn
      Gary Wrinn answered on Aug 5, 2014 7:13 PM

      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.

      • 0
        Stefan
        Stefan answered on Aug 6, 2014 8:29 AM

        Hello Gary,

        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.

      • 0
        Gary Wrinn
        Gary Wrinn answered on Aug 6, 2014 3:42 PM

        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!

      • 0
        Stefan
        Stefan answered on Aug 8, 2014 8:39 AM

        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.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
infrawtf
Favorites
0
Replies
11
Created On
Aug 08, 2014
Last Post
11 years, 6 months ago

Suggested Discussions

Tags

Created by

Created on

Aug 8, 2014 8:39 AM

Last activity on

Feb 24, 2026 9:23 AM