微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

解决方法

有很多方法.让我逐一解释.
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>

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

2.如果要跟踪所有键,而不管按下哪个键,那么最好使用

<TextBox x:Name="tBoxCouponSearch" Text="{Binding SearchMatchHomeorVisitor,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文件夹中的interaction.dll和intereactivity.dll(你会得到)在C盘中的程序文​​件中安装Blend时的那些dll.

3.如果是这样的话就像在一个特定的键上调用函数或者按下其他键上的一些其他函数调用.然后你必须在代码后面做.

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

通过这种方式,您可以捕获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;
            }
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐