jlp*_*jlp 11 wpf keyboard-shortcuts mvvm
我有遵循MVVM模式的WPF应用程序.我需要实现键盘快捷键.这些快捷方式必须控制WebBrowser控件的行为.我定义了第一个自定义命令并添加到视图的inputbindings.将有更多的命令,他们将不得不在浏览器上调用脚本:
MainWindow.xaml.cs:
...
CommandBinding cb = new CommandBinding(RemoteControlCommands.TestCommand, MyCommandExecuted, MyCommandCanExecute);
this.CommandBindings.Add(cb);
KeyGesture kg = new KeyGesture(Key.Q, ModifierKeys.Control);
InputBinding ib = new InputBinding(RemoteControlCommands.TestCommand, kg);
this.InputBindings.Add(ib);
}
private void MyCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
webBrowser.InvokeScript("foo", "Hello World!");
}
private void MyCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将其融入MVVM模式?MVVM对我来说是一个新概念,但我理解如何将视图命令绑定到视图模型并执行方法或更改属性.但是,在这种情况下我需要的是在视图中的控件上执行一个方法.在这种情况下,快捷处理的最佳位置是什么?
Aka*_*ava 44
<Window.InputBindings>
<KeyBinding Command="{Binding MyCommand, Source=viewModel...}"
CommandParameter="{Binding,ElementName=browserControl,Mode=Self}"
Gesture="CTRL+R" />
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)
您可以将命令属性绑定到View Model的命令.
| 归档时间: |
|
| 查看次数: |
11359 次 |
| 最近记录: |