WPF:命令的文本框而触发,当我们打到它回车键? [英] WPF: Command for TextBox which fires up when we hit Enter Key on It?

查看:597
本文介绍了WPF:命令的文本框而触发,当我们打到它回车键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家都知道,这是很容易绑定按钮在WPF与命令的在应用服务我们的视图模型类。

不过,假设我有一个文本框,我需要将其绑定到一个命令它会调用时,我打在回车键文本框 ??

是否有可能?如是?请帮我..

目前我使用的是下面的事件,但它看起来丑陋... 我不能把它的视图模型

 私人无效TextBox_KeyUp(对象发件人,KeyEventArgs E)
        {
            如果(e.Key == System.Windows.Input.Key.Enter)
            {
                //你的事件在这里处理程序
                e.Handled =真实;
                的MessageBox.show(回车键为pressed !!!);
            }
        }
 

解决方案

雅利安人,不是每个WPF对象支持指挥。所以,如果你wan't要做到这一点,你需要或者打电话给你的视图模型从code的后面(有点耦合)或使用一些MVVM消息实现脱钩的。请参见 MVVM光通讯工具一个例子。或者简单的使用触发器是这样的:

 <文本框>
    <我:Interaction.Triggers>
        <我:EventTrigger中事件名称=的KeyUp>
            <我:InvokeDataCommand命令={结合MyCommand}/>
        < /我:EventTrigger中>
    < /我:Interaction.Triggers>
< /文本框>
 

As we all know, it is very easy to bind Buttons in WPF apps with the Command's in our VIEWMODEL class.

But suppose I have a TextBox and I need to bind it to a command which fires up when I hit the Enter key on TextBox??

Is it Possible? If yes? please help me..

Currently I'm Using following event but it looks ugly... and I can't put it in VIEWMODEL class

private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                // your event handler here
                e.Handled = true;
                MessageBox.Show("Enter Key is pressed!!!");
            }
        }

解决方案

Aryan, not every WPF object supports commanding. So if you wan't to do that you'll need either to call your view model from your code behind (a little coupled) or use some MVVM Messaging implementation to decouple that. See MVVM Light Messaging toolkit for an example. Or simple use triggers like this:

<TextBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyUp">
            <i:InvokeDataCommand Command="{Binding MyCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

这篇关于WPF:命令的文本框而触发,当我们打到它回车键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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