xaml - WPF MVVM - Activate ViewModel-command by use of hot keys from MainWindow -


i have mvvm application in mainwindow contains grid several views.

in 1 of viewmodels there command can activate using hot keys in corresponding view. can activate command when placed in part of mainwindow contains specific view.

the following code works fine, if want able activate command in specific view:

componentview.xaml:

    ...     <usercontrol.inputbindings>         <keybinding gesture="ctrl+u" command="{binding path=uploadcmd}"/>     </usercontrol.inputbindings> </usercontrol> 


able activate command using hot keys part of mainwindow.

this failed attempt keybinding in mainwindow, command can activated anywhere:

mainwindow.xaml:

<window x:class="myprogram.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:view="clr-namespace:myprogram.view"         xmlns:vm="clr-namespace:myprogram.viewmodel"         ...>     <grid>         // grid content     </grid>     <window.datacontext>         <vm:componentviewmodel>             <keybinding gesture="ctrl+u" command="{binding path=uploadcmd}"/>         </vm:componentviewmodel>     </window.datacontext> </window> 

is possible somehow let application know command directly in xaml?

three options can think of:

  1. you place commands in main window , use relative source binding child views command in main window.

    {binding path=pathtoproperty, relativesource={relativesource ancestortype={x:type window}}}

  2. if using mvvm framework, there way message / communicate between viewmodels. not best solution can used in right circumstance.

  3. use ioc / dependecy injection create service instantiated singleton in viewmodellocator. inject service implementation parent , child viewmodels constructors. can call functions/properties service parent viewmodel can accessed child viewmodels. use navigation purposes.

what trying commands?

hope helps


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -