recursivebytes

Code secure: int main(){return 0;}

CommandBinding with dynamic Shortcut Support

29.06.2015

In MVVM you normally don´t use Click-Event like in WinForms, Java Swing or other traditional GUI-Framework. On of the aims of MVVM is to reduce the binding between presentation and program logic. The way you do bind Actions to a button (or other control) is by an implementation of the ICommand interface, that is hold by the ViewModel. The View binds to this property to create the link between implementation and triggering of the command. A common implementation is the RelayCommand class, which supports a basic functionality of this.

The implementation

In my applications I improved this by adding a flexable support for shortcut-bindings, which allows me to (re)bind the Menu- and Button-Command shortcuts at runtime. This is used to allow the user to change the default shortcuts in the configuration window.

The ViewModel implementation is quite the same like the RelayCommand Class:

Binding in the View works like this:

Changing the shortcut at runtime is really simple: just assign a different key and/or modifier to the object hold by the NewFileCommand-Property. Due to the INotifyPropertyChanged, the View gets informed about the change.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)