从WPF(MVVM)中的View中将KeyEventArgs传递给ViewModel

Run*_*jee 7 .net silverlight wpf mvvm

我有一个文本框,我试图将KeyEventArgs从视图传递到viewmodel.但我不知道如何实现它.基本上我需要的是如果输入一些特殊字符,那么如果键入普通文本(如A,B,C..etc),则调用某些函数,然后调用其他函数,如果按下Enter键,则其他函数将被调用.如何在MVVM中执行.我在VS 2012中使用WPF.

Ani*_*jee 17

有很多方法.让我逐一解释.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>
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,您可以看到单击这些特定键,这些命令将被执行并传递给viewmodel.然后像往常一样在viewmodel中调用函数.

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>
Run Code Online (Sandbox Code Playgroud)

现在这将触发所有按键或按键事件..你想要调用的任何函数都可以在viewmodel中调用.(这样做包括项目的Debug文件夹中的interaction.dll和intereactivity.dll(你会得到)在C盘中的程序文​​件中安装Blend时的那些dll.

3.如果是某个特定键上的函数被调用或按下其他键的其他一些函数被调用.那么你必须在代码后面做.

private void Window_KeyUp_1(object sender, KeyEventArgs e)
        {
            try
            {
                mainWindowViewModel.KeyPressed = e.Key;
Run Code Online (Sandbox Code Playgroud)

通过这种方式你可以捕获keyeventargs .. mainWindowViewModel是viewModel的一个实例.现在在viewmodel中你喜欢这样

private Key _keyPressed ;
        public Key KeyPressed
        {
            get
            {
                return _keyPressed;
            }
            set
            {
                _keyPressed = value;
                OnPropertyChanged("KeyPressed");
            }
        }
Run Code Online (Sandbox Code Playgroud)

现在,Viewmodel以下列方式实现此属性

bool CanSearchTextBox
        {
            get
            {
                if (KeyPressed != Key.Up && KeyPressed != Key.Down && KeyPressed != Key.Left && KeyPressed != Key.Right && MatchSearchList!=null)
                    return true;
                else
                    return false;
            }
        }
Run Code Online (Sandbox Code Playgroud)