Pre*_*cog 24
您还可以创建一个通用行为,可以轻松应用于应用程序中的任何文本框.这是一个示例行为类: -
public class TextBoxEnterKeyUpdateBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
if (this.AssociatedObject != null)
{
base.OnAttached();
this.AssociatedObject.KeyDown += AssociatedObject_KeyDown;
}
}
protected override void OnDetaching()
{
if (this.AssociatedObject != null)
{
this.AssociatedObject.KeyDown -= AssociatedObject_KeyDown;
base.OnDetaching();
}
}
private void AssociatedObject_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
if (e.Key == Key.Return)
{
if (e.Key == Key.Enter)
{
textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要在xaml中使用此类,只需将其包含在文本框行为集合中,如下所示: -
<TextBox>
<i:Interaction.Behaviors>
<TextBoxEnterKeyUpdateBehavior />
</i:Interaction.Behaviors>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
这里"i"指的是System.Windows.Interactivity命名空间.
| 归档时间: |
|
| 查看次数: |
12364 次 |
| 最近记录: |