Keybinding a RelayCommand

后端 未结 6 796
有刺的猬
有刺的猬 2020-12-03 13:52

I\'m using the RelayCommand in my app. It\'s great for putting the code in the viewmodel, but how do I bind keystrokes to my command?

RoutedUICommand has its InputGe

6条回答
  •  庸人自扰
    2020-12-03 14:11

    I create a Key binding in my view model that links the command and the Key like so

            this.KeyBinding = new KeyBinding();
            //set the properties
            this.KeyBinding.Command = this.Command;
            this.KeyBinding.Key = this.Key;
            //modifier keys may or may not be set
            this.KeyBinding.Modifiers = this.ModifierKeys;
    

    then i create a collection of InputBinding items in my View Model root and add them to the windows InputBindings in my window's code behind

         foreach (var item in applicationViewModel.InputBindingCollection) {
            this.InputBindings.Add(item);
         }
    

    its bad form to do stuff in the code behind i know, but i dont know how to do the binding yet, however im still working on it. :) The only thing this doent give me is a key command modifer list in the menu, but that is to come.

提交回复
热议问题