【问题标题】:Get event args in view model of a wpf program在 wpf 程序的视图模型中获取事件参数
【发布时间】:2015-01-30 05:40:45
【问题描述】:

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

【问题讨论】:

    标签: wpf mvvm keyboard-events wpf-4.0 mvvm-toolkit


    【解决方案1】:
    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.
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2011-09-13
      • 2021-06-20
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      相关资源
      最近更新 更多