UWP XAML文本框对焦后进入

问题描述:

我有这样的菜单:UWP XAML文本框对焦后进入

enter image description here

我倒是喜欢,如果光标在ValorInsTextBox(Valor的文本框),我按回车键,应用程序调用按钮InserirBtn_ClickAsync(Inserir Button),并且在该过程之后,光标会返回到PosicaoInsTextBox(PosiçãoTextbox)。 我做了一些使用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); 
     } 
    } 
} 

当我调试的代码,我按Enter键时ValorInsTextBox是重点,方法ValorInsTextBox_KeyDown开始,一切顺利。当它到达在线:

PosicaoInsTxtBox.Focus(FocusState.Programmatic); 

它去执行方法PosicaoTextBox_KeyDown并开始执行它。我不知道为什么!任何人都可以帮助我?

您可以在KeyRoutedEventArgs的Handled属性设置为true在ValorInsTxtBox_KeyDown事件处理程序,以防止被调用的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中按ENTER键时再次调用“TextBox:

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; 
} 
+0

这解决了问题,非常感谢! –