Overriding Ctrl-Z and Ctrl-Y for NumericUpDown

5 posts, 0 answers
  1. Georg
    Georg avatar
    15 posts
    Member since:
    Nov 2007

    Posted 16 Oct 2018 Link to this post

    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>

     

     

  2. Georg
    Georg avatar
    15 posts
    Member since:
    Nov 2007

    Posted 16 Oct 2018 Link to this post

    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.
  3. Georg
    Georg avatar
    15 posts
    Member since:
    Nov 2007

    Posted 16 Oct 2018 in reply to Georg Link to this post

    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. 
  4. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1018 posts

    Posted 19 Oct 2018 Link to this post

    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.
  5. Georg
    Georg avatar
    15 posts
    Member since:
    Nov 2007

    Posted 19 Oct 2018 in reply to Dilyan Traykov Link to this post

    Thanks Dilyan. That works like a charm!
Back to Top