从WPF(MVVM)中的视图将KeyEventArgs传递到ViewModel [英] Pass KeyEventArgs to ViewModel from View in WPF (MVVM)

查看:43
本文介绍了从WPF(MVVM)中的视图将KeyEventArgs传递到ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

有很多方法.让我一一解释.1.如果您只有一些选定的键,并且在按下这些选定的键时仅要实现某些功能,那么最好的方法是以下

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

<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>

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

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.如果要跟踪所有按键,而与按下哪个按键无关,那么更好地使用

2.if all key are to be tracked irrespective of the fact which key is pressed then better to use

<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>

现在,这将在所有按键或按键事件时触发..您要调用的任何函数都可以在viewmodel中调用.(为此,请在项目的Debug文件夹中包含interact.dll和intereactivity.dll(在Blending安装到C驱动器的程序文件中后,您将获得这些dll.

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.

3.如果是这种情况,例如要调用某个功能上的特定键或按下其他键时要调用其他功能,则必须在后面的代码中进行操作.

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.

private void Window_KeyUp_1(object sender, KeyEventArgs e)
        {
            try
            {
                mainWindowViewModel.KeyPressed = e.Key;

通过这种方式,您可以捕获keyeventargs .. mainWindowViewModel是viewModel的一个实例.现在,在视图模型中,您确实会这样

in this way you can catch the keyeventargs .. mainWindowViewModel is an instance of viewModel. Now in viewmodel you do like this

private Key _keyPressed ;
        public Key KeyPressed
        {
            get
            {
                return _keyPressed;
            }
            set
            {
                _keyPressed = value;
                OnPropertyChanged("KeyPressed");
            }
        }

现在,在Viewmodel中,该属性通过以下方式实现

Now in Viewmodel implement this property in the following way

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

这篇关于从WPF(MVVM)中的视图将KeyEventArgs传递到ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆