C# 在 Silverlight 中的 TextBox 上按 Enter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/312922/
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
Pressing Enter on TextBox in Silverlight
提问by Buddy Lindsey
I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".
我正在开发一个 Silverlight 应用程序,您需要在文本框中输入信息,然后按 Enter。好吧,没有我能找到的 onclick 事件,所以我所做的是使用 onkeypressup 事件并检查它是否是按下的 Enter 键,如果是这样的话“等等”。
It just feels like there is a better way to do this. So the question is, is there?
只是感觉有一种更好的方法可以做到这一点。那么问题来了,有吗?
采纳答案by ptio
I thinks that's the way to catch Key.Enter.
我认为这是捕获 Key.Enter 的方法。
Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.
此外,如果您使用 KeyDown 事件而不是 KeyUp 事件,您的代码将更具可读性。
If you only care about catching Key.Enter for a single control then your approach is correct.
如果您只关心为单个控件捕获 Key.Enter,那么您的方法是正确的。
You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").
您还可以通过使用其容器的 KeyDown 事件(“事件冒泡”)来捕获一组相关控件的 Key.Enter。
回答by Shawn Wildermuth
Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.
你真的想要它在文本框中吗?我会在容器(例如网格、画布)上放置一个 onkeyup 处理程序,以在表单上的任何位置按下按钮。
回答by vmachacek
Well, Im preaty new to Silverlight and I created HitEnter beahaviour for button which have one DependencyProperty Button.
好吧,我是 Silverlight 的新手,我为具有一个 DependencyProperty 按钮的按钮创建了 HitEnter 行为。
And I manulay wire up Button and Behavior (in code behind) and then when enter is hit I inovke the command on the button.
我手动连接按钮和行为(在后面的代码中),然后当按下 Enter 键时,我会调用按钮上的命令。
回答by Dublx
This will work if you use want to bind the Command property instead of using the Click event. Start by creating an event handler for Click (see below) and then in the KeyUp do:
如果您使用想要绑定 Command 属性而不是使用 Click 事件,这将起作用。首先为 Click 创建一个事件处理程序(见下文),然后在 KeyUp 中执行以下操作:
private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) SomeButton_Click(this, null);
}
private void SomeButton_Click(object sender, RoutedEventArgs e)
{
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
{
cmd.Execute(null);
}
}
回答by slfan
I use the following implementation when using the command pattern:
使用命令模式时,我使用以下实现:
private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression b = MyTextBox.GetBindingExpression(TextBox.TextProperty);
if (b != null)
b.UpdateSource();
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
cmd.Execute(null);
}
}
When you press Enter, the data source of the textbox is not updated and the command uses an old value. Therefore you have to call UpdateSource before executing the command.
当您按 Enter 时,文本框的数据源不会更新并且命令使用旧值。因此,您必须在执行命令之前调用 UpdateSource。
Of course you can catch the event on a higher level than the textbox.
当然,您可以在比文本框更高的级别上捕获事件。