捕获 TextBox 中的 Enter 键 [英] Capturing the Enter key in a TextBox
问题描述
在我的 WPF 视图中,我试图将一个事件与 Enter 键相关联,如下所示:
In my WPF view, I am trying to tie an event to the Enter key as follows:
<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>
此代码有效,当用户按下 Enter 键时,我的 EnterKeyCommand 会触发.但是,问题是当事件触发时,WPF 尚未将文本框中的文本绑定到SearchCriteria".因此,当我的事件触发时,SearchCriteria"的内容为空白.是否可以在此代码中进行简单的更改,以便在 EnterKey 命令触发时获取文本框的内容?
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?
推荐答案
您需要将 TextBox.Text
绑定上的 UpdateSourceTrigger
更改为 PropertyChanged代码>.请参阅此处.
You need to change the UpdateSourceTrigger
on your TextBox.Text
binding to PropertyChanged
. See here.
这篇关于捕获 TextBox 中的 Enter 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!