This is a migrated thread and some comments may be shown as answers.

Overriding Ctrl-Z and Ctrl-Y for NumericUpDown

4 Answers 365 Views
NumericUpDown
This is a migrated thread and some comments may be shown as answers.
Georg
Top achievements
Rank 1
Veteran
Georg asked on 16 Oct 2018, 04:11 PM

An earlier post explains how to set IsUndoEnabled=false to prevent RadNumericUpDown from handling undo. I did that, and it disabled the local undo feature. However, when focus is on the control and I press Ctrl-Z or Ctrl-Y, my global commands are not executed, although they work when focus is not on the RadNumericUpDown.

 

I tried two approaches to resolve this: adding a KeyDown handler, and adding local input bindings. The KeyDown handler receives the LControl key, but not the Z or Y keys while LControl is pressed. The local input bindings don't seem to do anything (see below).

So, after setting IsUndoEnabled=false, how do I get Ctrl-Z and Ctrl-Y to execute my global commands when focus is on the RadNumericUpDown control?

 

 

  <telerik:RadNumericUpDown   VerticalAlignment="Center"
         NumberDecimalDigits="2"
         Margin="0"
         Width="80"
         Loaded="RadNumericUpDown_Loaded"
         Value="{Binding Width}">
   <telerik:RadNumericUpDown.InputBindings>
    <KeyBinding Gesture="Ctrl+Z" Command="{Binding UndoCommand}" />
    <KeyBinding Gesture="Ctrl+Y" Command="{Binding RedoCommand}" />
   </telerik:RadNumericUpDown.InputBindings>
  </telerik:RadNumericUpDown>

 

 

4 Answers, 1 is accepted

Sort by
0
Georg
Top achievements
Rank 1
Veteran
answered on 16 Oct 2018, 05:20 PM
I have found a work-around though: Set UpdateValueEvent="PropertyChanged", add a ValueChanged handler, and then put the extra logic needed in the ValueChanged handler. The control handles undo/redo, but the extra logic we need still gets invoked.
0
Georg
Top achievements
Rank 1
Veteran
answered on 16 Oct 2018, 05:36 PM
The work-around isn't perfect though, since now undo/redo in the RadNumericUpDown only works when it has focus, and not through the Undo/Redo menu items. 
0
Dilyan Traykov
Telerik team
answered on 19 Oct 2018, 09:59 AM
Hello Georg,

What I can suggest in order to achieve the desired result is to handle the PreviewKeyDown event of the control in a similar manner:

private void RadNumericUpDown_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var upDown = sender as RadNumericUpDown;
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        var vm = upDown.DataContext as MyViewModel;
        if (e.Key == Key.Z)
        {
            vm.UndoCommand.Execute(null);
        }
        else if (e.Key == Key.Y)
        {
            vm.RedoCommand.Execute(null);
        }
    }
}

Could you please give this approach a try and let me know if it works for you?

I look forward to your reply.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Georg
Top achievements
Rank 1
Veteran
answered on 19 Oct 2018, 01:26 PM
Thanks Dilyan. That works like a charm!
Tags
NumericUpDown
Asked by
Georg
Top achievements
Rank 1
Veteran
Answers by
Georg
Top achievements
Rank 1
Veteran
Dilyan Traykov
Telerik team
Share this question
or