如何在 windows phone 8.1 中设置选项卡索引

How to set tab Index in windows phone 8.1

我的应用程序页面中有 3 个文本框,我想为它们设置选项卡索引,以便当用户按下键盘上的 return 键时,它应该转到下一个文本框。 我已经设置了 TextBox 的标签索引 属性 但它不起作用。

这是一个可能的实现。

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

它适用于 windows phone 8.1 应用,代码如下。

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

在所有TextBox的KeyDown事件中使用上述方法