在 wpf 程序的视图模型中获取事件参数



>我有一个程序,我需要从键盘键入的字符访问到视图模型中。我可以使用KeyUp方法在代码后面轻松获取KeyEventArgs,但是如何在ViewModel中访问它呢?请帮忙。

You need the following to pass keyEventArgs to viewmodel
1. Interaction triggers in xaml because native input binding don't support keyeventargs
<textblock>
<i:interactiontriggers>
<i:action eventname ="keyup">
<multibinding command={binding somecommand, converter={staticresource eventconverter}}">
<binding ...>
<binding...>
</multibing>
</i:action>
</i:interactiontriggers>
</textblock>
2  Create a custom command  (example, relay command) where you can initialize the command like below in viewmodel
var cmd = SomeCommand;
Customcommand c = new RelayCommand<KeyEventArgs>(cmd.Execute, cmd.CanExecute);
3. In the command action, you can access the mouseeventargs or keyeventargs.
If you look up more, you'll get an idea.

相关内容

  • 没有找到相关文章

最新更新