Ticket T612298
Visible to All Users

Detect when enter key is pressed in a grid cell

created 7 years ago

I have a very similar problem like in this ticket
https://www.devexpress.com/Support/Center/Question/Details/Q467204/wpf-detect-when-enter-key-is-pressed-in-a-grid
but mine is different because I want to catch that event on another level.
I have some custom control and use it as edit template for a column in the grid. The problem is that my control is unable to get PreviewKeyDown event because it is handled somewhere above in the hierarchy. I investigated where that happens and it is a lightweight cell editor which is a part of your grid control.
Is there a way my custom control could know when Enter key is pressed and do something it is supposed to do?

Show previous comments (1)

    Asume you have an external control. It handles PreviewKeyDown event inside. Now I want to use that control in my grid for editing cell value. I need to use that control and I can't substitute it with some other control.

    Andrey Marten (DevExpress Support) 7 years ago

      Hello Povilas,

      For this, it should be sufficient to use DataTemplate with your custom control in GridColumn's CellTemplate (see the attached project). However, only BaseEdit class descendants can be used for editing purposes in CellTemplate and the use of a custom control in this template will cause synchronization and navigation side effects. For example, you may not able to navigate and start editing cells in this columns by using only the Keyboard. That is why I'm asking for more information about how this custom control is implemented and which behavior you are trying to achieve by using it. This way we can try to find a better solution.

      Thanks,
      Andrey

        Can I use that edit template thing for columns instead of cell template? In that case I don't have to have that control derived from BaseEdit. Something like this:

        XAML
        <dxg:GridColumn> <dxg:GridColumn.EditTemplate> <ControlTemplate> <MyCustomControl x:Name="PART_Editor"/> </ControlTemplate> </dxg:GridColumn.EditTemplate> </dxg:GridColumn>

        Answers approved by DevExpress Support

        created 7 years ago (modified 7 years ago)

        Hello,
        In general, if you need to use a completely custom control, you can use the EditTemplate property. Although this approach may still produce side effects, it's more suitable for such situations than the approach with the CellTemplate property.
        In your case, the Enter key press is handled at the TextEdit class level, since this editor is used as the default editor for our cells and GridColumn's EditTemplate property places your custom control to this editor's EditTemplate. So, to accomplish this task, you can create a TextEdit class descendant and override its NeedsEnter method in the following manner:

        C#
        public class TextEditEx : TextEdit { protected override bool NeedsEnter(ModifierKeys modifiers) { if (modifiers == ModifierKeys.None) return true; return base.NeedsEnter(modifiers); } }

        I've attached a simple sample demonstrating the main idea of this approach. Please take a moment to review it.

        Thanks,
        Kirill

          Show previous comments (1)
          Andrey Marten (DevExpress Support) 7 years ago

            Hello,

            I suggest that you try using our FocusBehavior in an element that should be focused in your custom control when it is loaded. I've modified the project to illustrate how it works. Please try the same in your application and let me know your results.

            Thanks,
            Andrey

              Thank you! Works like a charm :)

              Andrey Marten (DevExpress Support) 7 years ago

                That's great! You are always welcome!
                Andrey

                Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.