【问题标题】:UWP XAML Textbox Focus after Enter输入后的 UWP XAML 文本框焦点
【发布时间】:2017-05-16 00:46:10
【问题描述】:

我有一个这样的菜单:

如果光标在 ValorInsTextBox(Valor 文本框)上并且我按 Enter,应用程序会调用按钮 InserirBtn_ClickAsync(Inserir 按钮),并且在此过程之后,光标会返回到 PosicaoInsTextBox(Posição 文本框)。 我使用 Key_Down 做了一些方法,但是发生了一些奇怪的事情。看代码:

private void PosicaoInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
    {
        InserirBtn_ClickAsync(sender, e);

        PosicaoInsTxtBox.Focus(FocusState.Programmatic);
    }
}

private void ValorInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
    {
        InserirBtn_ClickAsync(sender, e);

        if (PosicaoInsTxtBox.IsEnabled)
        {
            PosicaoInsTxtBox.Focus(FocusState.Programmatic);
        }
        else
        {
            ValorInsTxtBox.Focus(FocusState.Programmatic);
        }
    }
}

当我调试代码时,当 ValorInsTextBox 处于焦点时按 Enter 键,并且 ValorInsTextBox_KeyDown 方法启动并且一切顺利。当它上线时:

PosicaoInsTxtBox.Focus(FocusState.Programmatic);

它去执行方法 PosicaoTextBox_KeyDown 并开始执行它。我不知道为什么!谁能帮帮我?

【问题讨论】:

    标签: c# xaml textbox uwp focus


    【解决方案1】:

    您可以在 ValorInsTxtBox_KeyDown 事件处理程序中将 KeyRoutedEventArgs 的 Handled 属性设置为 true,以防止调用 PosicaoInsTxtBox_KeyDown 事件处理程序:

    private void ValorInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Enter)
        {
            InserirBtn_ClickAsync(sender, e);
    
            if (PosicaoInsTxtBox.IsEnabled)
            {
                PosicaoInsTxtBox.Focus(FocusState.Programmatic);
            }
            else
            {
                ValorInsTxtBox.Focus(FocusState.Programmatic);
            }
        }
        e.Handled = true;
    }
    

    并在 PosicaoInsTxtBox_KeyDown 事件处理程序中执行相同操作,以防止在 Posicao" TextBox 中按 ENTER 时再次调用它:

    private void PosicaoInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Enter)
        {
            InserirBtn_ClickAsync(sender, e);
    
            PosicaoInsTxtBox.Focus(FocusState.Programmatic);
        }
        e.Handled = true;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2018-06-10
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      相关资源
      最近更新 更多