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