将 KeyEventArgs 从 WPF (MVVM) 中的视图传递给 ViewModel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19345192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass KeyEventArgs to ViewModel from View in WPF (MVVM)
提问by Runi Chatterjee
I have a textbox and I am trying to pass KeyEventArgs from view to viewmodel .But I do not know how to implement it. Basically what I need is if some special character is typed then some function is to be called if normal text( like A,B,C..etc) are typed then some other function is to be called and if Enter key is pressed then some other function is to be called.How to do it in MVVM . I am using WPF with VS 2012.
我有一个文本框,我试图将 KeyEventArgs 从 view 传递到 viewmodel 。但我不知道如何实现它。基本上我需要的是,如果输入了一些特殊字符,那么如果输入普通文本(如 A、B、C.. 等),则调用某些函数,然后调用其他一些函数,如果按下 Enter 键,则调用某些函数其他函数将被调用。如何在 MVVM 中做到这一点。我在 VS 2012 中使用 WPF。
回答by Anindya
There are many approach.Let me explain all one by one. 1.If you have only some selected key and on pressing those selected key only some function are to be implemented then the best approach is the following
方法很多,我一一解释。1.如果您只有一些选定的键,并且在按下这些选定的键时只能实现某些功能,那么最好的方法是以下
<TextBox x:Name="tboxCouponSearch" Text="{Binding SearchMatchHomeorVisitor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource EfesInputTextbox}" Width="170" Height="26" AcceptsReturn="False" TabIndex="40" TextWrapping="NoWrap" KeyDown="tboxCouponSearch_KeyDown_1">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SearchTextboxEnterKeyCommand}"/>
<KeyBinding Key="Left" Command="{Binding LeftRightUpDownARROWkeyPressed}" />
<KeyBinding Key="Down" Command="{Binding LeftRightUpDownARROWkeyPressed}" />
<KeyBinding Key="Up" Command="{Binding LeftRightUpDownARROWkeyPressed}" />
<KeyBinding Key="Right" Command="{Binding LeftRightUpDownARROWkeyPressed}" />
</TextBox.InputBindings>
</TextBox>
in the above example you can see on click of those specific key those commands are to be executed and passed to the viewmodel. then in viewmodel as usual you can call the functions.
在上面的例子中,你可以看到点击那些特定的键,这些命令将被执行并传递给视图模型。然后像往常一样在视图模型中您可以调用这些函数。
2.if all key are to be tracked irrespective of the fact which key is pressed then better to use
2.如果不管按下哪个键,都要跟踪所有键,那么最好使用
<TextBox x:Name="tboxCouponSearch" Text="{Binding SearchMatchHomeorVisitor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource EfesInputTextbox}" Width="170" Height="26" AcceptsReturn="False" TabIndex="40" TextWrapping="NoWrap" KeyDown="tboxCouponSearch_KeyDown_1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding SearchTextBoxCommand}" CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Now this will fire upon all key down or key up events.. and any function you would like to call you can call in viewmodel.(to do this include interaction.dll and intereactivity.dll in Debug folder of the project( you will get those dll upon installation of Blend in program file in C drive.
现在这将在所有按键按下或按键按下事件时触发..并且您想调用的任何函数都可以在视图模型中调用。(为此,项目的调试文件夹中包括交互.dll和intereactivity.dll(您将获得在 C 驱动器的程序文件中安装 Blend 时的那些 dll。
3.if it is the case like on a particular key on function is to be called or on key press of other key some other function to be called.then you have to do in code behind.
3.如果是这样的情况,比如在某个特定的键上要调用函数,或者在其他键的按键上要调用其他一些函数。那么你必须在后面的代码中做。
private void Window_KeyUp_1(object sender, KeyEventArgs e)
{
try
{
mainWindowViewModel.KeyPressed = e.Key;
in this way you can catch the keyeventargs .. mainWindowViewModel is an instance of viewModel. Now in viewmodel you do like this
通过这种方式,您可以捕获 keyeventargs .. mainWindowViewModel 是 viewModel 的一个实例。现在在视图模型中你这样做
private Key _keyPressed ;
public Key KeyPressed
{
get
{
return _keyPressed;
}
set
{
_keyPressed = value;
OnPropertyChanged("KeyPressed");
}
}
Now in Viewmodel implement this property in the following way
现在在 Viewmodel 中通过以下方式实现这个属性
bool CanSearchTextBox
{
get
{
if (KeyPressed != Key.Up && KeyPressed != Key.Down && KeyPressed != Key.Left && KeyPressed != Key.Right && MatchSearchList!=null)
return true;
else
return false;
}
}

