在wpf中,如何获取选项卡顺序中的下一个控件

发布于 2024-11-03 01:16:09 字数 174 浏览 2 评论 0原文

我知道如何将焦点设置到 Tab 键顺序中的下一个控件,但我实际上并不想更改焦点...我只想 获取 Tab 键顺序中的下一个控件(也许还可以获取上一个、第一个和最后一个控件。)那么...howyadodat?

中号

I know how to set the focus to the next control in the tab order, but I don't actually want to change focus... I just want to get the next control in the tab order (perhaps get the previous, first and last ones too.) So... howyadodat?

M

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风和你 2024-11-10 01:16:09

PredictFocus(FocusNavigationDirection.Next) 不起作用,正如 @Cameron 所说。我基于 @Randolpho 的帖子的包装器代码现在运行良好。

我尝试了几种模式,最后得出结论,我必须确保 container 实际上是 e 的父级之一,以避免意外结果。

/// <summary>
/// Get next tab order element.
/// </summary>
/// <param name="e">The element to get next tab order</param>
/// <param name="container">The container element owning 'e'. Make sure this is a container of 'e'.</param>
/// <param name="goDownOnly">True if search only itself and inside of 'container'; otherwise false.
/// If true and next tab order element is outside of 'container', result in null.</param>
/// <returns>Next tab order element or null if not found</returns>
public DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly)
{
    var navigation = typeof(FrameworkElement)
        .GetProperty("KeyboardNavigation", BindingFlags.NonPublic | BindingFlags.Static)
        .GetValue(null);

    var method = navigation
        .GetType()
        .GetMethod("GetNextTab", BindingFlags.NonPublic | BindingFlags.Instance);

    return method.Invoke(navigation, new object[] { e, container, goDownOnly }) as DependencyObject;
}

前任。)

var nextElement = GetNextTab(textbox1, window, false);

PredictFocus(FocusNavigationDirection.Next) doesn't work as @Cameron said. My wrapper code based on @Randolpho 's post is now working well.

I tried several patterns and finally concluded I have to make sure container is actually one of the parents of e to avoid unexpected results.

/// <summary>
/// Get next tab order element.
/// </summary>
/// <param name="e">The element to get next tab order</param>
/// <param name="container">The container element owning 'e'. Make sure this is a container of 'e'.</param>
/// <param name="goDownOnly">True if search only itself and inside of 'container'; otherwise false.
/// If true and next tab order element is outside of 'container', result in null.</param>
/// <returns>Next tab order element or null if not found</returns>
public DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly)
{
    var navigation = typeof(FrameworkElement)
        .GetProperty("KeyboardNavigation", BindingFlags.NonPublic | BindingFlags.Static)
        .GetValue(null);

    var method = navigation
        .GetType()
        .GetMethod("GetNextTab", BindingFlags.NonPublic | BindingFlags.Instance);

    return method.Invoke(navigation, new object[] { e, container, goDownOnly }) as DependencyObject;
}

Ex.)

var nextElement = GetNextTab(textbox1, window, false);
单身情人 2024-11-10 01:16:09

我认为不可能按 Tab 键顺序拥有下一个控件,但您可以循环子集合并使用 KeyboardNavigation.GetIsTabStop(..) KeyboardNavigation.GetTabIndex()< /code> 为他们创建一个助手。

I don't think is possible to have the next control in tab order, but you can loop on the children collection and use the KeyboardNavigation.GetIsTabStop(..) KeyboardNavigation.GetTabIndex() to create an helper for thet.

疯到世界奔溃 2024-11-10 01:16:09

PredictFocus 为应该在 .NET 4 中获得 Tab 支持,但是 当前MSDN 暗示还没有。然而,这可能是一个文件疏忽;我没有尝试过,但你可以尝试一下。

如果这不起作用,KeyboardNavigation 上有一个私有方法可能会对您有所帮助;您必须使用反射来调用它,并且需要适当的代码访问权限才能执行此操作,但它可能会起作用。 .NET Reflector 显示签名如下:

private DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly)

其中 e 是您要获取其下一个选项卡的元素,container 是其父容器。我不是 100% 确定 goDownOnly 会做什么,但我猜测它表明您不想离开父容器。如果该元素没有下一个选项卡,该方法将返回 null。

请记住,这是一个私有方法;下一个版本很容易发生变化。

编辑:您需要一个KeyboardNavigation实例!完全忘记了这一点。 FrameworkElement.KeyboardNavigation 中有一个静态的,但它也是内部的,因此需要通过反射来获取它。

PredictFocus was supposed to get Tab support in .NET 4, but the current MSDN implies it hasn't. That may be a documentation oversight, however; I haven't tried it, but you could give it a shot.

If that doesn't work, there's a private method on KeyboardNavigation that may do you some good; you'll have to use reflection to call it, and you'll need the proper code access permissions to do it, but it might work. .NET Reflector reveals the signature as follows:

private DependencyObject GetNextTab(DependencyObject e, DependencyObject container, bool goDownOnly)

Where e is the element for which you want to get the next tab, and container is its parent container. I'm not 100% sure what goDownOnly does, but I guess that it indicates that you don't want to leave the parent container. The method will return null if there isn't a next tab for that element.

Keep in mind, this is a private method; highly susceptible to change come the next version.

Edit: you'll need an instance of KeyboardNavigation! Totally forgot about that. There's a static one at FrameworkElement.KeyboardNavigation, but it's also internal, so reflection to get at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文