Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 将KeyEventArgs从WPF(MVVM)中的视图传递到ViewModel_.net_Wpf_Silverlight_Mvvm - Fatal编程技术网

.net 将KeyEventArgs从WPF(MVVM)中的视图传递到ViewModel

.net 将KeyEventArgs从WPF(MVVM)中的视图传递到ViewModel,.net,wpf,silverlight,mvvm,.net,Wpf,Silverlight,Mvvm,我有一个文本框,我试图将KeyEventArgs从视图传递到viewmodel,但我不知道如何实现它。基本上,我需要的是,如果键入了一些特殊字符,然后调用一些函数,如果键入了普通文本(如A、B、C…等),然后调用一些其他函数,如果按下Enter键,则调用一些其他函数。如何在MVVM中执行此操作。我将WPF与VS 2012一起使用。有许多方法。让我逐一解释。 1.如果您只有一些选定的键,并且按下这些选定的键时,只有一些功能需要实现,那么最好的方法如下 <TextBox x:Name="tbo

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

有许多方法。让我逐一解释。 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>
通过这种方式,您可以捕获keyeventargs。。mainWindowViewModel是viewModel的一个实例。 现在在viewmodel中,您可以这样做

private Key _keyPressed ;
        public Key KeyPressed
        {
            get
            {
                return _keyPressed;
            }
            set
            {
                _keyPressed = value;
                OnPropertyChanged("KeyPressed");
            }
        }
现在,在Viewmodel中,按以下方式实现此属性

bool CanSearchTextBox
        {
            get
            {
                if (KeyPressed != Key.Up && KeyPressed != Key.Down && KeyPressed != Key.Left && KeyPressed != Key.Right && MatchSearchList!=null)
                    return true;
                else
                    return false;
            }
        }
private Key _keyPressed ;
        public Key KeyPressed
        {
            get
            {
                return _keyPressed;
            }
            set
            {
                _keyPressed = value;
                OnPropertyChanged("KeyPressed");
            }
        }
bool CanSearchTextBox
        {
            get
            {
                if (KeyPressed != Key.Up && KeyPressed != Key.Down && KeyPressed != Key.Left && KeyPressed != Key.Right && MatchSearchList!=null)
                    return true;
                else
                    return false;
            }
        }