在TextBox中确定Enter键
考虑Win Phone 7中的XAML TextBox。
<TextBox x:Name="UserNumber" />
这里的目标是,当用户按下屏幕上的键盘上的Enterbutton时,会启动一些逻辑来刷新屏幕上的内容。
我想为Enter提供一个特别的事件。 这可能吗?
- 特定于TextBox的事件,还是系统键盘事件?
- 是否需要检查每个按键上的
Enter? 即一些模拟到ASCII 13? - 什么是编码这个要求的最好方法?

在文本框中的一个简单的方法是
private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { Debug.WriteLine("Enter"); } }
你会寻找实现特定于该文本框的KeyDown事件,并检查KeyEventArgs是否被按下(如果它匹配Key.Enter,则执行某些操作)
<TextBox Name="Box" InputScope="Text" KeyDown="Box_KeyDown"></TextBox> private void Box_KeyDown(object sender, KeyEventArgs e) { if (e.Key.Equals(Key.Enter)) { //Do something } }
请注意,在WP7仿真器的Beta版本中,尽pipe使用屏幕上的软件键盘可以正确检测到Enter键,但如果使用硬件键盘(通过按暂停/中断键激活),Enter键似乎会出现通过作为Key.Unknown – 或者至less,这是在我的电脑上这样做…
如果您不想将任何代码添加到您的XAML代码背后的代码,并保持从MVVM体系结构点干净,您可以使用以下方法。 在你的XAML中,像这样定义你的命令:
<TextBox Text="{Binding Text}" custom:KeyUp.Command="{Binding Path=DataContext.DoCommand, ElementName=root}" />
其中KeyUp类:
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
命名空间PhoneGuitarTab.Controls
{
公共静态类KeyUp
{
私有静态只读DependencyProperty KeyUpCommandBehaviorProperty = DependencyProperty.RegisterAttached(
“KeyUpCommandBehavior”
typeof运算(TextBoxCommandBehavior),
typeof运算(KEYUP),
空值);
///
///执行KeyUp事件的命令。
///
公共静态只读DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
“命令”,
typeof运算(ICommand的),
typeof运算(KEYUP),
新的PropertyMetadata(OnSetCommandCallback));
///
///命令参数提供命令执行。
///
公共静态只读DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
“CommandParameter”
的typeof(对象),
typeof运算(KEYUP),
新的PropertyMetadata(OnSetCommandParameterCallback));
///
///设置在KeyUp事件上执行。
///
/// TextBox依赖对象来附加命令
///命令附加
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesAsParameters”,Justification =“仅适用于buttonbase”)]
public static void SetCommand(TextBox textBox,ICommand command)
{
textBox.SetValue(CommandProperty,command);
}
///
///检索附加到。
///
///包含Command依赖项属性的TextBox
///附加命令的值
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesAsParameters”,Justification =“仅适用于buttonbase”)]
公共静态ICommand GetCommand(文本框文本框)
{
返回textBox.GetValue(CommandProperty)作为ICommand;
}
///
///设置提供的CommandParameter附加属性的值。
///
///文本框来附加CommandParameter
///要附加的参数值
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesAsParameters”,Justification =“仅适用于buttonbase”)]
public static void SetCommandParameter(TextBox textBox,object parameter)
{
textBox.SetValue(CommandParameterProperty,参数);
}
///
///获取提供的CommandParameter附加属性中的值
///
///具有CommandParameter的TextBox
///属性的值
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesAsParameters”,Justification =“仅适用于buttonbase”)]
公共静态对象GetCommandParameter(TextBox文本框)
{
返回textBox.GetValue(CommandParameterProperty);
}
private static void OnSetCommandCallback(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs e)
{
TextBox textBox = dependencyObject作为TextBox;
if(textBox!= null)
{
TextBoxCommandBehavior behavior = GetOrCreateBehavior(textBox);
behavior.Command = e.NewValue作为ICommand;
}
}
private static void OnSetCommandParameterCallback(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs e)
{
TextBox textBox = dependencyObject作为TextBox;
if(textBox!= null)
{
TextBoxCommandBehavior behavior = GetOrCreateBehavior(textBox);
behavior.CommandParameter = e.NewValue;
}
}
私人静态TextBoxCommandBehavior GetOrCreateBehavior(TextBox文本框)
{
TextBoxCommandBehavior behavior = textBox.GetValue(KeyUpCommandBehaviorProperty)as TextBoxCommandBehavior;
如果(行为== null)
{
行为=新的TextBoxCommandBehavior(文本框);
textBox.SetValue(KeyUpCommandBehaviorProperty,behavior);
}
回报行为;
}
}
}
class级使用额外的,所以我也提供。 TextBoxCommandBehavior类:
使用系统;
使用System.Windows.Controls;
使用System.Windows.Input;
命名空间PhoneGuitarTab.Controls
{
公共类TextBoxCommandBehavior:CommandBehaviorBase
{
public TextBoxCommandBehavior(TextBox textBoxObject)
:base(textBoxObject)
{
textBoxObject.KeyUp + =(s,e)=>
{
string input =(s as TextBox).Text;
// TODO在这里validation用户input
** //input被按下!**
如果((e.Key == Key.Enter)
&&(!String.IsNullOrEmpty(input)))
{
this.CommandParameter = input;
ExecuteCommand();
}
};
}
}
}
CommandBehaviorBase类:
使用系统;
使用System.Windows.Controls;
使用System.Windows.Input;
命名空间PhoneGuitarTab.Controls
{
///
///处理连接到一个命令的基本行为。
///
///目标对象必须从Control派生
///
/// CommandBehaviorBase可以用来提供类似于的新行为。
///
公共类CommandBehaviorBase
T:控制
{
私人ICommand命令;
私人对象commandParameter;
private readonly WeakReference targetObject;
私人只读EventHandler commandCanExecuteChangedHandler;
///
///指定目标对象的构造函数。
///
///行为所附加的目标对象。
公共CommandBehaviorBase(T targetObject)
{
this.targetObject = new WeakReference(targetObject);
this.commandCanExecuteChangedHandler = new EventHandler(this.CommandCanExecuteChanged);
}
///
///执行并监视相应的命令
///
公共ICommand命令
{
得到{返回命令; }
组
{
如果(this.command!= null)
{
this.command.CanExecuteChanged - = this.commandCanExecuteChangedHandler;
}
this.command = value;
如果(this.command!= null)
{
this.command.CanExecuteChanged + = this.commandCanExecuteChangedHandler;
UpdateEnabledState();
}
}
}
///
///执行期间提供命令的参数
///
公共对象CommandParameter
{
得到{return this.commandParameter; }
组
{
如果(this.commandParameter!= value)
{
this.commandParameter = value;
this.UpdateEnabledState();
}
}
}
///
///这个行为被附加到的对象。
///
受保护的T TargetObject
{
得到
{
返回targetObject.Target as T;
}
}
///
///根据命令的执行能力更新目标对象的IsEnabled属性。
///
受保护的虚拟无效UpdateEnabledState()
{
如果(TargetObject == null)
{
this.Command = null;
this.CommandParameter = null;
}
否则if(this.Command!= null)
{
TargetObject.IsEnabled = this.Command.CanExecute(this.CommandParameter);
}
}
私人无效CommandCanExecuteChanged(对象发件人,EventArgs e)
{
this.UpdateEnabledState();
}
///
///执行命令,如果已设置,则提供
///
受保护的虚拟无效ExecuteCommand()
{
如果(this.Command!= null)
{
this.Command.Execute(this.CommandParameter);
}
}
}
}
您可以在我的开源项目( PhoneGuitarTab.Controls项目的解决scheme)中find工作示例: http : //phoneguitartab.codeplex.com
如果你正在使用模拟器,你也可以做这样的事情来检测物理键盘上的input键。
private void textBox1_KeyUp(object sender, KeyEventArgs e) { var isEnterKey = e.Key == System.Windows.Input.Key.Enter || e.PlatformKeyCode == 10; if (isEnterKey) { // ... } }
禁用键盘
面临同样的问题; 上面的例子只给出了关于如何捕获键盘按下事件(它回答了问题)的细节,但是为了禁用键盘,点击或input,我只把焦点设置到另一个控件上。
这将导致禁用键盘。
private void txtCodeText_KeyDown(object sender, KeyEventArgs e) { if(e.Key.Equals(Key.Enter)) { //setting the focus to different control btnTransmit.Focus(); } }