gpt4 book ai didi

c# - 当我们按下 Enter 键时触发的 WPF TextBox 命令

转载 作者:IT王子 更新时间:2023-10-29 03:41:57 24 4
gpt4 key购买 nike

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

我有一个 TextBox,我需要将它绑定(bind)到一个 Command,当我按下 EnterTextBox 是重点。目前,我正在为 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!");
}
}

有更好的方法吗?

最佳答案

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

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

关于c# - 当我们按下 Enter 键时触发的 WPF TextBox 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6882196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com