Replacing an EditorCommand

SyntaxEditor for WPF Forum

Posted 12 years ago by Kasper
Version: 12.1.0561
Avatar

Hi guys,

Just a quick question: I have made my own version of the MoveToNextWordAction, which inherits from MoveToNextWordAction and overrides the Execute() method, and I wish to replace the existing one in the SyntaxEditor control with my own version. I need to do it in code, not XAML, since it should be replaced only if the user has selected it in the Settings dialog. I have looked at the InputBindings property, but I'm still not sure what the best way to do this is. Please enlighten me :)

Comments (6)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Kasper,

The SyntaxEditor.ResetCommandBindings() method is where the EditActions.MoveToNextWordAction() is being bound to the MoveToNextWord command. In this line the binding is added:

this.CommandBindings.Add(new EditActions.MoveToNextWordAction().CreateCommandBinding(EditorCommands.MoveToNextWord));

Here you can replace the EditAction with your own remade version.

[Modified 12 years ago]


Actipro Software Support

Posted 12 years ago by Kasper
Avatar

Hi guys,

I might be missing something obvious here, but which code is that? I don't own the SyntaxEditor source, so if that's what you're talking about, I can't do it. I was thinking that I could remove the binding and then add my own, I just wasn't sure of the best approach to do it and where exactly to look.

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Kasper,

You can search through the CommandBindings collection for the binding that specifies the command. Remove it and add another one like the line I posted above.


Actipro Software Support

Posted 12 years ago by Kasper
Avatar

Hi,

Your first reply makes a lot more sense after it was edited, but I'm afraid I still can't get it working. I do wonder why I have to remove the binding from the editor control and then add it to the Window (this) instead? Anyway, to avoid further confusions, I will send you a small application that shows what I'm trying to do. After removing the original command and adding my own, nothing happens when I use the keyboard shortcut (Ctrl+Right). Hopefully you can tell me what I'm doing wrong :)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Kasper,

As you figured out in the sample you sent, I had meant that you should remove the appropriate CommandBinding from the editor, then add your own to the editor, not the Window. Your problem was caused by you searching for and removing the InputBinding connected to MoveToNextWord, not the CommandBinding. This is why no action was performed when you used the keyboard shortcut, because it was no longer bound with the InputBinding. If you go through your code and change the places where you have InputBinding to CommandBinding then it will work fine. Just for you information, I did notice your custom MoveToNextWord throws an IndexOutOfRange exception when you try to use the shortcut at the end of the text. You may have already noticed this, but I figured I'd let you know.


Actipro Software Support

Posted 12 years ago by Kasper
Avatar

Hi,

Ah yes of course, what a silly mistake. I think I was simply re-using some code I had tested elsewhere, which is why I was messing with the InputBindings instead of the CommandBindings. Thanks for noticing it :)

And thank you for telling me about the IndexOutOfRange. The code was not tested yet, because I wasn't able to add the command, but it has been fixed now :)

The latest build of this product (v24.1.3) was released 26 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.