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?
Detect when enter key is pressed in a grid cell
Answers approved by DevExpress Support
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
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
Hello,
At first glance, the use of preview events at the level of your GridControl and checking the OriginalSource in corresponding event args should be sufficient. At the same time, it's difficult to provide a precise solution based only on the information from your description. Would you please send us a simple sample showing your current implementation? This will help us find a more suitable solution.
Additionally, describe the behavior that you are trying to achieve by using a custom EditTempalte. Perhaps, we can suggest a better way to achieve the same functionality. I hope to hear from you soon.
Thanks,
Andrey
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.
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:
<dxg:GridColumn> <dxg:GridColumn.EditTemplate> <ControlTemplate> <MyCustomControl x:Name="PART_Editor"/> </ControlTemplate> </dxg:GridColumn.EditTemplate> </dxg:GridColumn>