当我们按 Enter 键时启动的 WPF 文本框命令

发布于 2024-11-27 05:46:45 字数 615 浏览 1 评论 0原文

将 WPF 应用程序中的 Button 绑定到 VIEWMODEL 类中的 Command 非常容易。我想为 TextBox 实现类似的绑定。

我有一个 TextBox,我需要将其绑定到一个 Command,当我按下 Enter 时,该命令会启动,而 TextBox > 重点突出。目前,我正在使用以下处理程序来处理 KeyUp 事件,但它看起来很丑...... 我无法将其放入我的 VIEWMODEL 类中。

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

有更好的方法吗?

It is very easy to bind Buttons in WPF apps to Commands in a VIEWMODEL class. I'd like to achieve a similar binding for a TextBox.

I have a TextBox and I need to bind it to a Command that fires up when I hit Enter while the TextBox is focused. Currently, I'm using the following handler for the KeyUp event, but it looks ugly...
and I can't put it in my 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!");
    }
}

Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

地狱即天堂 2024-12-04 05:46:45

我遇到了同样的问题并找到了解决方案 这里,这是代码示例:

<TextBox>
  <TextBox.InputBindings>
    <KeyBinding Command="{Binding Path=CmdSomething}" Key="Enter" />
  </TextBox.InputBindings>
</TextBox>

I've faced with the same problem and found solution here, here is the code sample:

<TextBox>
  <TextBox.InputBindings>
    <KeyBinding Command="{Binding Path=CmdSomething}" Key="Enter" />
  </TextBox.InputBindings>
</TextBox>
べ映画 2024-12-04 05:46:45

Aryan,并非每个 WPF 对象都支持命令。因此,如果您不想这样做,则需要从后面的代码调用视图模型(有点耦合),或者使用一些 MVVM 消息传递实现来解耦。有关示例,请参阅 MVVM Light Messaging 工具包。或者简单地使用这样的触发器:

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

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>
旧城烟雨 2024-12-04 05:46:45

我喜欢 Sarh 的答案,但它在我的程序中不起作用,除非我将 Enter 更改为 Return

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Key="Return" Command="{}" />
   </TextBox.InputBindings>
</TextBox>

I like Sarh's answer, but it wouldn't work in my program, unless I changed Enter to Return:

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Key="Return" Command="{}" />
   </TextBox.InputBindings>
</TextBox>
仙女山的月亮 2024-12-04 05:46:45
<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding AddCommand}" Key="Return" />
    </TextBox.InputBindings>
</TextBox>

我从此处获取答案

<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding AddCommand}" Key="Return" />
    </TextBox.InputBindings>
</TextBox>

I took answer from here

遗心遗梦遗幸福 2024-12-04 05:46:45

您可以将 AcceptReturn 属性设置为 true。

 <TextBox AcceptsReturn="True" />

You can set true to AcceptReturn property.

 <TextBox AcceptsReturn="True" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文