如何在调用 CheckBox.Focus() 时使 CheckBox 焦点边框出现?
当用户按 Tab 键进入复选框以赋予其焦点时,复选框周围会出现虚线边框以指示它具有焦点。
当 CheckBox 通过调用 myCheckBox.Focus()
的代码获得焦点时,不会出现此类指示器(即使按空格键会切换状态)。
当我以编程方式聚焦复选框时,如何使复选框焦点边框出现?
When the user tabs into a CheckBox to give it focus, a dotted border appears around the CheckBox to indicate that it has focus.
When the CheckBox gets focused by code calling myCheckBox.Focus()
, no such indicator appears (even though pressing the space bar toggles the state).
How can I make the CheckBox focus border appear when I have programmatically focused the CheckBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当您使用键盘(Tab 键)导航时才有意显示边框。有关此主题的 MSDN 页面提供了更多详细信息:
如果你想显示边框,你可以使用 IsFocused- 属性上的触发器来进行一些视觉更改(尽管你不能用它设置边框)或者如果你确实想要边框,您必须创建自己的 ControlTemplate。
还有一个 在这里讨论一个有点相关的主题,其中建议是模拟按键,但我建议不要使用这个解决方案来解决您的问题。
The border is intentionally only shown if you are navigating by the keyboard (Tab key). The MSDN page on this topic has further details:
If you want to show a border, you could use a Trigger on the IsFocused- Property to do some visual changes (although you can't set the border with this) or if you actually want a border, you would have to create your own ControlTemplate.
There is also a thread here on SO on a somewhat related topic where the suggestion is to simulate a key press, but I would suggest not to use this solution for your problem.
通过编辑
KeyboardNavigationEx
来自 ControlzEx 的文件我设法解决了这个问题(一如既往,完全归功于 punker76)。只需调用
KeyboardHelper.Focus
方法并传递应聚焦的UIElement
(例如KeyboardHelper.Focus(myCheckBox)
),这是
KeyboardHelper类:
By editing the
KeyboardNavigationEx
file from ControlzEx I managed to solve the issue (full credit goes, as always, to punker76).Just call the
KeyboardHelper.Focus
method passing theUIElement
that shoud be focused (e.g.KeyboardHelper.Focus(myCheckBox)
)Here's the
KeyboardHelper
class: