简体   繁体   English

当文本框“跳转”到文本框WPF网格时,激发事件KeyDown出现问题

[英]Issue with firing event KeyDown on TextBox when it's 'jumping' to a TextBox WPF Grid

I have a Grid which contains like few TextBoxes below each other : 我有一个网格,彼此之间包含几个文本框:

在此处输入图片说明

By pressing Enter I need to jump from txt1 to txt2, from txt2 to txt3 and so on, and I solved it like this: 通过按Enter键,我需要从txt1跳到txt2,从txt2跳到txt3,依此类推,我这样解决了它:

private void Grid_PreviewKeyDown_1(object sender, KeyEventArgs e)
{
    try
    {
        if (e.Key == Key.Enter)
        {
            UIElement element = e.Source as UIElement;

            element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            //e.Handled = true;

        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

And my issue is here, on each of txt boxes by pressing Enter I should calculate something from another txtBoxes, and when I press enter on txt1 it is gonna jump on txt2 and immediatelly call txt2_KeyDown event, which I want to avoid.. I want to call txt2_KeyDown event when I'm 'standing' on txt2 and when I press enter.. 我的问题在这里,在每个txt框上,按Enter键,我应该从另一个txtBoxes计算内容,当我在txt1上按Enter键时,它将跳转到txt2并立即调用txt2_KeyDown事件,我想避免这一点。当我“站在” txt2上并按Enter时,调用txt2_KeyDown事件。

ie: 即:

private void txt2_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        try
        {

            CalculateSomethingFromOtherTextBoxes();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

This above is being fired when I am 'standing' on txt1 and when I want to jump to txt2 by pressing Enter, how can I just 'jump' without calling txt2 KeyDown event If I did not press Enter while I am really 'standing' on 'txt2'. 当我“站立”在txt1上并且想通过按Enter跳到txt2时,会触发以上内容,我怎么能在不调用txt2 KeyDown事件的情况下“跳转”,如果我在真正站立时没有按Enter在“ txt2”上。

Thanks guys, Cheers 谢谢大家,干杯

The problem is because of a KeyDown event that you attached to textboxes. 问题是由于您附加到文本框的KeyDown事件。

Instead, remove this event and add PreviewKeyDown event. 而是,删除此事件并添加PreviewKeyDown事件。

So, your method signature should be something like this: 因此,您的方法签名应如下所示:

private void txt2_PreviewKeyDown(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Return)
        {
            try
            {

               CalculateSomethingFromOtherTextBoxes();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
}

I tested it and it is working as expected. 我测试了它,并且按预期工作。

And my issue is here, on each of txt boxes by pressing Enter I should calculate something from another txtBoxes, and when I press enter on txt1 it is gonna jump on txt2 and immediatelly call txt2_KeyDown event, which I want to avoid.. I want to call txt2_KeyDown event when I'm 'standing' on txt2 and when I press enter.. 我的问题在这里,在每个txt框上,按Enter键,我应该从另一个txtBoxes计算内容,当我在txt1上按Enter键时,它将跳转到txt2并立即调用txt2_KeyDown事件,我想避免。当我“站在” txt2上并按Enter时,调用txt2_KeyDown事件。

Then you could handle the LostKeyboardFocus event of "txt2" instead of handling the KeyDown event: 然后,您可以处理“ txt2”的LostKeyboardFocus事件,而不是处理KeyDown事件:

private Key key;
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    key = e.Key;
    if (key == Key.Enter)
    {
        UIElement element = e.OriginalSource as UIElement;
        element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }

}

private void txt2_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    if (key == Key.Enter)
    {
        CalculateSomethingFromOtherTextBoxes();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM