Silverlight 3 中的自动选项卡 [英] Auto-tab in Silverlight 3

查看:23
本文介绍了Silverlight 3 中的自动选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在 SL3 应用程序中从一个控件自动切换到下一个控件".例如,一个 TextBox 被限制为 3 个字符——在输入第三个字符时,焦点应该自动移动到表单上的下一个控件(我的实际用法略有不同,但这个例子就足够了).

I have a requirement to be able to auto-tab from one control to the "next control" in a SL3 app. For example, a TextBox is limited to 3 characters - on typing the 3rd character the focus should automatically move to the next control on the form (my actual usage is slightly different but that example suffices).

但是,由于 SL 自动确定选项卡顺序,除了逆向工程/复制 Silverlight 的逻辑来确定可视化树中的哪个控件应该是下一个获得焦点的控件之外,似乎没有其他方法可以做到这一点.

However, as SL automatically determines the tab sequence there doesn't seem to be a way of doing this apart from reverse engineering/duplicating Silverlight's logic to figure out which control in the visual tree should be the next control to gain focus.

有人已经实现了吗?

推荐答案

我一直在寻找一个相当普遍的解决方案 - 但我已经能够处理一些相当具体的事情 - 基本上它使用 VisualTreeHelper 来查找具有与我想要在其旁边使用 Tab 的控件相同的父级,并将焦点设置为该控件.

I was looking for a fairly generalised solution - but I've been able to make do with something fairly specific - basically it uses the VisualTreeHelper to find children with the same parent as the control that I want to tab next to, and sets focus to that.

与必须遍历我的所有控件(这是一个相当大的 LOB 应用程序)并为每个控件配置下一个"控件相比,这是一个更可口的解决方案.

It's a more palatable solution than having to go through all my controls (and this is for a fairly large LOB application) and configure the "next" control for each of them.

这是我的代码,以防它对其他人有帮助.(VisualTreeeHelperUtil 是我自己的一个类,为 VisualTreeHelper 添加了一些实用功能)

Here's my code, in case it helps somebody else. (VisualTreeeHelperUtil is a class of my own that adds some utility functions to VisualTreeHelper)

public static void TabNext(DependencyObject parentElement, Control fromControl)
{
    var children = VisualTreeHelperUtil.FindChildren<Control>(parentElement).
        Where(c => c.IsEnabled && c.IsTabStop && c.Visibility == Visibility.Visible).
        ToList();

    if (children.Contains(fromControl))
    {
        var thisIndex = children.IndexOf(fromControl);
        var targetIndex = thisIndex + 1;
        if (children.Count > targetIndex)
        {
            var targetChild = children[targetIndex];
            fromControl.Dispatcher.BeginInvoke(() =>
               {
                   targetChild.Focus();
                   var txt = targetChild as TextBox;
                   if (txt != null)
                   {
                       txt.SelectAll();
                   }
               });
        }
    }
}

这篇关于Silverlight 3 中的自动选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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