在wpf中,如何获取选项卡顺序中的下一个控件
我知道如何将焦点设置到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PredictFocus(FocusNavigationDirection.Next)
不起作用,正如 @Cameron 所说。我基于 @Randolpho 的帖子的包装器代码现在运行良好。我尝试了几种模式,最后得出结论,我必须确保
container
实际上是e
的父级之一,以避免意外结果。前任。)
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 ofe
to avoid unexpected results.Ex.)
我认为不可能按 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.PredictFocus
为应该在 .NET 4 中获得 Tab 支持,但是 当前MSDN 暗示还没有。然而,这可能是一个文件疏忽;我没有尝试过,但你可以尝试一下。如果这不起作用,
KeyboardNavigation
上有一个私有方法可能会对您有所帮助;您必须使用反射来调用它,并且需要适当的代码访问权限才能执行此操作,但它可能会起作用。 .NET Reflector 显示签名如下:其中
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:Where
e
is the element for which you want to get the next tab, andcontainer
is its parent container. I'm not 100% sure whatgoDownOnly
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 atFrameworkElement.KeyboardNavigation
, but it's also internal, so reflection to get at it.