WPF FlexGrid Home/End Keys with Active Editor

Posted by: frugamas on 10 March 2022, 10:44 am EST

    • Post Options:
    • Link

    Posted 10 March 2022, 10:44 am EST

    I’m working with the .Net6 version of the WPF FlexGrid and have an issue with the grid’s handling of the Home and End keys.

    When a cell is being editing in an edit control, if I want to move the cursor to the end of the text in the control, the data grid moves the focus to the last column in the current row. The same thing happens with the Home key.

    You can see the behavior in the FlexGridExplorer.

    Repro Steps:

    1. Open the FlexGridExplorer.
    2. Select the Getting Started tab.
    3. Double-click the Address cell on the first row of the grid.
    4. Use the Left Arrow key to move the insert caret to the begin of the text.
    5. Hit End key.
    6. BUG: The cell in the [b]e-mail[/b" column is now the active cell.

    How do I prevent the Flexgrid from handling the Home and End key’s while a cell is being edited?

    Thanks,

    Felix

  • Posted 10 March 2022, 9:10 pm EST

    Hi Felix,

    You can prevent the the flexgrid from handling the Home and End key’s while a cell is in edit mode by handling PreviewKeyDown as.(see code snippet)

    
            private void Grid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == Key.Home | e.Key == Key.End)
                {
                    e.Handled = true;
                    grid.FinishRowEditing();
                }
            }
    
    

    Please refer the attached sample for the same: FlexgridHomeEndKeyPrevention.zip

    Best Regards,

    Nitin

  • Posted 16 March 2022, 4:37 am EST

    Hi Nitin,

    Thanks for the reply. What I really need if for the edit control to receive the events. I know that I can’t manipulate the caret and selection on the edit control in response to the keyboard event, but it’s not ideal.

    So, there’s no way for the flexgrid to ignore(not inrerecept) these events?

    Thanks,

    Felix

  • Posted 16 March 2022, 6:25 pm EST

    Hi Felix,

    You can handle these events on editors, for that you need to handle PrepareCellForEdit event and then handle the key events on editor. Now you can manipulate caret and selection by on TextBox editor by getting it from PrepareCellForEdit event.

    
            private void Grid_PrepareCellForEdit(object? sender, C1.WPF.Grid.GridCellEditEventArgs e)
            {
                if(e.Editor is TextBox)
                {
                    var txtBox = (TextBox)e.Editor;
                    txtBox.PreviewKeyDown += (s1, e1) =>
                     {
                         if (e1.Key == Key.Home | e1.Key == Key.End)
                         {
                             e1.Handled = true;
                             grid.FinishRowEditing();
                         }
                     };
                }
            }
    
    

    Please refer the attached modified sample for the same : FlexgridHomeEndKeyPrevention_Mod.zip

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels