“标签"使用 TabIndex 的字段之间 [英] "Tabbing" between fields using TabIndex

查看:26
本文介绍了“标签"使用 TabIndex 的字段之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用 .NET 4.5 的 Windows Phone 8 C#/XAML 应用程序中,我试图通过表单迭代".换句话说,当用户按下 "Enter" 键时,焦点会改变到另一个 TextBoxPasswordBox.

In my Windows Phone 8 C#/XAML Application using .NET 4.5 I'm trying to "iterate" through form. In other words, when the user presses "Enter" key, the focus changes to another TextBox or PasswordBox.

我的代码现在的样子

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 索引的文本框.我自己慢慢想通了(也通过在评论中与人讨论),但由于我花了很长时间才写了这个问题:)
  1. HOW TO SELECT NEXT ELEMENT TO FOCUS ON BY TABINDEX??? I know I should probably use LINQ, to select all textboxes in the usercontrol and then select the one with tabindex index. I'm slowly figuring it on my own (and also by discussing it with people in the comments), but as it's taking me a long time I wrote the question :)

*(另外,我不知道如何命名这个问题,如果你觉得你的名字更合适,可以随意重命名)

*(also, I was not sure how to name the question, feel free to rename it if you think your name fits it better)

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

非常感谢!:)

推荐答案

这是一个可能的实现.

<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.您可以在其中添加更多智能代码...

This next code assume that the ContentPanel contains only TextBox. It's up to you to add more smart code in it...

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

这篇关于“标签"使用 TabIndex 的字段之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆