wpf 捕获 TextBox 中的 Enter 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5556489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capturing the Enter key in a TextBox
提问by Hosea146
In my WPF view, I am trying to tie an event to the Enter key as follows:
在我的 WPF 视图中,我试图将一个事件与 Enter 键联系起来,如下所示:
<TextBox Width="240" VerticalAlignment="Center" Margin="2" Text="{Binding SearchCriteria, Mode=OneWayToSource}">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding EnterKeyCommand}"/>
<KeyBinding Key="Tab" Command="{Binding TabKeyCommand}"/>
</TextBox.InputBindings>
</TextBox>
This code works and my EnterKeyCommand fires when the users presses the Enter key. However, the problem is that when the event fires, WPF hasn't yet bound the text in the textbox to 'SearchCriteria'. So when my event fires, the contents of 'SearchCriteria' is blank. Is there a simple change I can make in this code so that I can get the contents of the textbox when my EnterKey command fires?
此代码有效,当用户按下 Enter 键时,我的 EnterKeyCommand 会触发。但是,问题是当事件触发时,WPF 尚未将文本框中的文本绑定到“SearchCriteria”。因此,当我的事件触发时,“SearchCriteria”的内容为空白。是否可以在此代码中进行简单的更改,以便在 EnterKey 命令触发时可以获取文本框的内容?
采纳答案by micahtan
回答by O.Man
You can do this by passing the TextBox's InputBindings
property as a CommandParameter
to the Command
:
您可以通过将 TextBox 的InputBindings
属性作为 aCommandParameter
传递给Command
:
<TextBox x:Name="MyTextBox">
<TextBox.InputBindings>
<KeyBinding Key="Return"
Command="{Binding MyCommand}"
CommandParameter="{Binding ElementName=MyTextBox, Path=Text}"/>
</TextBox.InputBindings>
</TextBox>
回答by K0D4
You can also do this in the code behind.
您也可以在后面的代码中执行此操作。
How to: Detect When the Enter Key Pressed
In the check for enter/return, just invoke your event handler code.
在检查输入/返回时,只需调用您的事件处理程序代码。
回答by sagneta
I know this is 6 years old but none of the answers produce the correct answer in its entirety using XAML and no code-behind.I still had some work to do. For reference the approach is the following. First the XAML
我知道这已经 6 年了,但没有一个答案使用 XAML 产生完整的正确答案,也没有代码隐藏。我还有一些工作要做。参考方法如下。首先是 XAML
<TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SearchEnterHit}"/>
<KeyBinding Key="Return" Command="{Binding SearchEnterHit}"/>
</TextBox.InputBindings>
</TextBox>
Basically the approach is that every key is tossed to the bound SearchTexton every keystroke. Thus the string will be entirely present within SearchText once the return/enter is pressed. Thus in the SearchEnterHitcommand the entire string within the TextBox is available via the SearchText property.
基本上,该方法是在每次击键时将每个键都扔到绑定的SearchText。因此,一旦按下返回/输入,字符串将完全存在于 SearchText 中。因此,在SearchEnterHit命令中,TextBox 中的整个字符串都可以通过 SearchText 属性获得。
As mentioned above the UpdateSourceTrigger=PropertyChanged is what flushes every keystroke to the SearchTextproperty. The KeyBindings capture the enter key.
如上所述,UpdateSourceTrigger=PropertyChanged 是将每次击键刷新到SearchText属性的原因。KeyBindings 捕获回车键。
This is really the simplest means of doing this with no code-behind and all XAML. Sure you are flushing keys to the SearchText property often but that generally is not a problem.
这确实是执行此操作的最简单方法,没有代码隐藏和所有 XAML。当然,您经常将键刷新到 SearchText 属性,但这通常不是问题。
回答by Developer
It is also can be done like async event. Here is the example.
它也可以像异步事件一样完成。这是示例。
tbUsername.KeyDown += async (s, e) => await OnKeyDownHandler(s, e);
private async Task OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
if (!string.IsNullOrEmpty(tbUsername.Text) && !string.IsNullOrEmpty(tbPassword.Password))
{
Overlay.Visibility = Visibility.Visible;
await Login();
}
}
}