如何在调用 CheckBox.Focus() 时使 CheckBox 焦点边框出现?

发布于 2024-10-25 17:59:24 字数 176 浏览 5 评论 0原文

当用户按 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 技术交流群。

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

发布评论

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

评论(3

岁月流歌 2024-11-01 17:59:24

仅当您使用键盘(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:

Focus visual styles act only when the
focus action was initiated by the
keyboard. Any mouse action or
programmatic focus change disables the
mode for focus visual styles.

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.

睡美人的小仙女 2024-11-01 17:59:24

通过编辑 KeyboardNavigationEx 来自 ControlzEx 的文件我设法解决了这个问题(一如既往,完全归功于 punker76)。

只需调用 KeyboardHelper.Focus 方法并传递应聚焦的 UIElement(例如 KeyboardHelper.Focus(myCheckBox)),

这是 KeyboardHelper类:

public sealed class KeyboardHelper
{
    private static KeyboardHelper _Instance;

    private readonly PropertyInfo _AlwaysShowFocusVisual;
    private readonly MethodInfo _ShowFocusVisual;

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static KeyboardHelper()
    {
    }

    private KeyboardHelper()
    {
        var type = typeof(KeyboardNavigation);

        _AlwaysShowFocusVisual = type.GetProperty("AlwaysShowFocusVisual", BindingFlags.NonPublic | BindingFlags.Static);
        _ShowFocusVisual = type.GetMethod("ShowFocusVisual", BindingFlags.NonPublic | BindingFlags.Static);
    }

    internal static KeyboardHelper Instance => _Instance ?? (_Instance = new KeyboardHelper());

    internal void ShowFocusVisualInternal()
    {
        _ShowFocusVisual.Invoke(null, null);
    }

    internal bool AlwaysShowFocusVisualInternal
    {
        get { return (bool)_AlwaysShowFocusVisual.GetValue(null, null); }
        set { _AlwaysShowFocusVisual.SetValue(null, value, null); }
    }

    public static void Focus(UIElement element)
    {
        element?.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            var keybHack = KeyboardHelper.Instance;
            var oldValue = keybHack.AlwaysShowFocusVisualInternal;

            keybHack.AlwaysShowFocusVisualInternal = true;

            try
            {
                Keyboard.Focus(element);
                keybHack.ShowFocusVisualInternal();
            }
            finally
            {
               keybHack.AlwaysShowFocusVisualInternal = oldValue;
            }
        }));
    }
}

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 the UIElement that shoud be focused (e.g. KeyboardHelper.Focus(myCheckBox))

Here's the KeyboardHelper class:

public sealed class KeyboardHelper
{
    private static KeyboardHelper _Instance;

    private readonly PropertyInfo _AlwaysShowFocusVisual;
    private readonly MethodInfo _ShowFocusVisual;

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static KeyboardHelper()
    {
    }

    private KeyboardHelper()
    {
        var type = typeof(KeyboardNavigation);

        _AlwaysShowFocusVisual = type.GetProperty("AlwaysShowFocusVisual", BindingFlags.NonPublic | BindingFlags.Static);
        _ShowFocusVisual = type.GetMethod("ShowFocusVisual", BindingFlags.NonPublic | BindingFlags.Static);
    }

    internal static KeyboardHelper Instance => _Instance ?? (_Instance = new KeyboardHelper());

    internal void ShowFocusVisualInternal()
    {
        _ShowFocusVisual.Invoke(null, null);
    }

    internal bool AlwaysShowFocusVisualInternal
    {
        get { return (bool)_AlwaysShowFocusVisual.GetValue(null, null); }
        set { _AlwaysShowFocusVisual.SetValue(null, value, null); }
    }

    public static void Focus(UIElement element)
    {
        element?.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            var keybHack = KeyboardHelper.Instance;
            var oldValue = keybHack.AlwaysShowFocusVisualInternal;

            keybHack.AlwaysShowFocusVisualInternal = true;

            try
            {
                Keyboard.Focus(element);
                keybHack.ShowFocusVisualInternal();
            }
            finally
            {
               keybHack.AlwaysShowFocusVisualInternal = oldValue;
            }
        }));
    }
}
心头的小情儿 2024-11-01 17:59:24
'initially set chkCheckBox.Appearance = 1

'on Got Focus set appearance = 0 - Flat
Private Sub chkCheckBox_GotFocus()
  chkCheckBox.Appearance = 0
End Sub

'on Lost Focus set appearance = 1 - 3D
Private Sub chkCheckBox_LostFocus()
  chkCheckBox.Appearance = 1
End Sub
'initially set chkCheckBox.Appearance = 1

'on Got Focus set appearance = 0 - Flat
Private Sub chkCheckBox_GotFocus()
  chkCheckBox.Appearance = 0
End Sub

'on Lost Focus set appearance = 1 - 3D
Private Sub chkCheckBox_LostFocus()
  chkCheckBox.Appearance = 1
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文