“在Tab之间使用TabIndex拖动”

问题描述 投票:0回答:4

问题

在我的使用.NET 4.5的Windows Phone 8 C#/ XAML应用程序中我正在尝试通过表单“ iteite”。换句话说,当用户按下“ Enter”键时,焦点将更改为另一个TextBoxPasswordBox

现在如何查看我的代码

XAML:

<TextBox TabIndex="0" KeyDown="TextBox_KeyDown"/>
<TextBox TabIndex="1" KeyDown="TextBox_KeyDown"/>
<TextBox TabIndex="2" KeyDown="TextBox_KeyDown"/>
<TextBox TabIndex="3" KeyDown="TextBox_KeyDown"/>
<TextBox TabIndex="4" KeyDown="TextBox_KeyDown"/>
....

C#背后的代码:

using System.Windows.Input;

private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key.Equals(Key.Enter))
    {
        int index = ((TextBox)sender).TabIndex + 1;
        //and here is what I've been missing
        //basically the code to select next tabindex
        //and set focus on it using Focus() method...
    }
}    

问题

  1. 如何通过TABINDEX选择要关注的下一个元素?我知道我应该使用LINQ来选择用户控件中的所有文本框,然后选择带有tabindex索引的文本框。我正在慢慢地自己弄清楚(也通过与评论者讨论),但是由于花了我很长时间,所以我写了一个问题:)

*(另外,我不确定如何命名该问题,如果您认为您的名字更合适,可以随时重命名)

基于SACHA答案的解决方案

LayoutRoot是用户控件的主网格

    private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key.Equals(Key.Enter))
        {
            int index = ((TextBox)sender).TabIndex + 1;
            var nextBox = LayoutRoot.Children.OfType<TextBox>().FirstOrDefault((x) => { return x.TabIndex == index; });

            if (nextBox != null)
            {
                nextBox.Focus();
            }
        }
    }

非常感谢! :)

c# xaml windows-phone-8
4个回答
6
投票

这是可能的实现。

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

4
投票

如果它对任何人都有帮助,在8.1(不知道它是否也适用于8)中,您可以这样做:

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

1
投票

您甚至都不需要命名ContentPanel

private void OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key.Equals(Key.Enter))
    {
        var currentBox = (TextBox)sender;
        var container = currentBox.Parent;
        var index = currentBox.TabIndex + 1;
        var nextBox = container.ChildrenOfType<TextBox>().FirstOrDefault(box => box.TabIndex == index);

        if (nextBox != null)
        {
            nextBox.Focus();
        }
    }
}

1
投票
protected void FocusOnTheNextTabIndex(object sender, FocusEventArgs e)
        {

            int nextIndex = e.VisualElement.TabIndex + 1;
            var element = sender as Element;
            View nextElement = VerifyParent(element, nextIndex);
            if (nextElement != null)
                nextElement.Focus();
        }

 public View VerifyParent(Element element, int index)
        {
            var parent = element.Parent;
            if (parent.GetType() == typeof(StackLayout))
            {
                var stackParent = parent as StackLayout;
                var response = VerifyChildren(stackParent, index);
                if (response != null)
                    return response;
                else
                    return VerifyParent(parent, index);
            }
            return null;
        }

        public View VerifyChildren(StackLayout stackLayout, int index)
        {
            foreach (View view in stackLayout.Children)
            {
                if (view.TabIndex == index)
                    return view;
                else if (view.GetType() == typeof(StackLayout))
                {
                    var stack = view as StackLayout;
                    if (stack.Children.Any())
                    {
                        var response = VerifyChildren(stack, index);
                        if (response != null)
                            return response;
                    }
                }
            }
            return null;
        }
© www.soinside.com 2019 - 2024. All rights reserved.