如何从 TextBox_KeyDown 事件中获取文本? WPF C#
How can I get the text from the TextBox_KeyDown event? WPF C#
我有这个事件,我想动态地(实时)从用户输入中获取文本,所以当用户输入任何字符时,它应该被添加到当前文本中,我有可能得到它.
public void TextBox_KeyDown(object sender, KeyEventArgs e) {
TextBox text = (TextBox) sender;
string myText = text.Text; // will return the old text in the textbox
}
如何获取新文本(使用用户添加的密钥)??
Usually the TextChanged event should be used to detect whenever the text in a TextBox or RichTextBox changes rather then KeyDown as you might expect.
如果您需要 KeyDown
事件,显然您不能依赖正在设置的文本。否则,您应该使用 TextChanged
事件。
编辑:
正如有人在评论中指出的那样,OnPreviewTextInput
could serve your purpose. You have a bit more control via the TextCompositionEventArgs
。
我有这个事件,我想动态地(实时)从用户输入中获取文本,所以当用户输入任何字符时,它应该被添加到当前文本中,我有可能得到它.
public void TextBox_KeyDown(object sender, KeyEventArgs e) {
TextBox text = (TextBox) sender;
string myText = text.Text; // will return the old text in the textbox
}
如何获取新文本(使用用户添加的密钥)??
Usually the TextChanged event should be used to detect whenever the text in a TextBox or RichTextBox changes rather then KeyDown as you might expect.
如果您需要 KeyDown
事件,显然您不能依赖正在设置的文本。否则,您应该使用 TextChanged
事件。
编辑:
正如有人在评论中指出的那样,OnPreviewTextInput
could serve your purpose. You have a bit more control via the TextCompositionEventArgs
。