Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass KeyEventArgs to ViewModel from View in WPF (MVVM)

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.

like image 724
Runi Chatterjee Avatar asked Oct 13 '13 12:10

Runi Chatterjee


1 Answers

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>

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

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

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;

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");
            }
        }

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;
            }
        }
like image 148
Anindya Avatar answered Sep 28 '22 08:09

Anindya