WPF如何使文本框输入后失去焦点 [英] WPF how to make textbox lose focus after hitting enter

查看:2163
本文介绍了WPF如何使文本框输入后失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些文本框,我希望用户输入十进制值。在我使用过的每个应用程序中,当我在文本框中输入内容并按回车时,该值被接受,文本框失去焦点。我怎样才能在我的应用程序?
我知道这对一个关键事件来说应该相对容易,但是也许有一个命令或者其他的东西。
我搜索了stackoverflow,但我只发现如何保持焦点后点击进入...

解决方案

也可以创建一个通用的行为,可以很容易地应用到您的应用程序中的任何文本框。下面是一个示例行为类: -

  public class TextBoxEnterKeyUpdateBehavior:Behavior< TextBox> 
{
protected override void OnAttached()
{
if(this.AssociatedObject!= null)
{
base.OnAttached();
this.AssociatedObject.KeyDown + = AssociatedObject_KeyDown;


$ b保护覆盖无效OnDetaching()
{
if(this.AssociatedObject!= null)
{
this.AssociatedObject.KeyDown - = AssociatedObject_KeyDown;
base.OnDetaching();


$ b $ 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));




$ b code $


要在xaml中使用这个类,只需将它包含在文本框行为集合中就可以了: -

$ p $ < TextBox> ;
< i:Interaction.Behaviors>
< TextBoxEnterKeyUpdateBehavior />
< / i:Interaction.Behaviors>
< / TextBox>

这里i是指System.Windows.Interactivity命名空间。


I created some textboxes and I want user to enter decimal values into them. In every application I have ever used, when I type something into the textbox and hit enter, the value is accepted and textbox lose focus. How can I do it in my app? I know it should be relatively easy to do it with a key event, but maybe there is a command or something. I searched the stackoverflow but I only found questions about how to keep focus after hitting enter...

解决方案

You can also create a generic behavior which can be easily applied to any textbox within your application. Here is a sample behavior class:-

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));
                }
            }
        }
    }
}

To use this class in your xaml, just include it in textbox behaviors collection like this :-

<TextBox>
    <i:Interaction.Behaviors>
           <TextBoxEnterKeyUpdateBehavior />
    </i:Interaction.Behaviors>
</TextBox>

Here "i" refers to System.Windows.Interactivity namespace.

这篇关于WPF如何使文本框输入后失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆