2016-01-21 27 views
0

我在我的應用程序頁面中有3個文本框,我想爲它們設置標籤索引,這樣當用戶按下鍵盤上的返回鍵時,它應該轉到下一個文本框。 我已經設置TextBox的Tab索引屬性,但它不工作。如何在Windows Phone 8.1中設置標籤索引

+0

[「使用TabIndex字段之間」Tabbing「可能重複](http://stackoverflow.com/questions/21229746/tabbing-between-fields-using-tabindex) – asitis

+0

您可以參考此問題http:// stackoverflow。 com/questions/21229746/tabbing-between-fields-using-tabindex – asitis

回答

1

它適用於windows phone 8.1應用程序與下面的代碼。

private void OnKeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Key==Windows.System.VirtualKey.Enter) 
    { 
     FocusManager.TryMoveFocus(FocusNavigationDirection.Next); 
    } 
} 

在所有文本框的KeyDown事件中使用上述方法。

0

這是一個可能的實現。

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" 
      > 
    <TextBox TabIndex="0" KeyDown="OnKeyDown"/> 
    <TextBox TabIndex="1" KeyDown="OnKeyDown"/> 
    <TextBox TabIndex="2" KeyDown="OnKeyDown"/> 
    <TextBox TabIndex="3" KeyDown="OnKeyDown"/> 
    <TextBox TabIndex="4" KeyDown="OnKeyDown"/> 
</StackPanel> 

下面的代碼假定ContentPanel僅包含TextBox。這取決於你添加更多的智能代碼...

private void OnKeyDown(object sender, KeyRoutedEventArgs e) 
{ 
    if (e.Key.Equals(Key.Enter)) 
    { 
     var txtBox = sender as TextBox; 
     var index = txtBox.TabIndex; 

     var nextTextBox = ContentPanel.Children.Cast<TextBox>().FirstOrDefault(t => t.TabIndex == index + 1); 

     if (nextTextBox != null) 
     { 
      nextTextBox.Focus(); 
     } 
    } 
} 
+0

我試過這個,但是它給出了一個錯誤,即KeyEventArgs不是system.windows的一個方法或屬性。輸入.. – Vinita

+0

我正在開發基本的Windows手機應用程序不Silverlight – Vinita

+0

使用此:KeyRoutedEventArgs – Harold

相關問題