How to adjust focus border of CheckBox when calling CheckBox.Focus ()?

When a user enters a CheckBox to give him focus, a dotted frame appears around the CheckBox to indicate that it has focus.

When the CheckBox focuses on the myCheckBox.Focus() code call, such an indicator does not appear (even if pressing the spacebar switches the state).

How can I create a CheckBox focus frame when I programmatically focused a CheckBox?

+4
source share
3 answers

The border is intentionally displayed only when navigating the keyboard (Tab key). The MSDN page on this topic contains additional information:

Focused visual styles only work when a keyboard focus has been triggered. Any mouse action or changing the focus of a program disables the mode for visual focus styles.

If you want to show a border, you can use the Trigger on the IsFocused-Property to make some visual changes (although you cannot set a border with this), or if you really want a border, you will have to create your own control pattern.

There is also a thread here on SO , on some related topic, which suggests simulating a keystroke, but I would suggest not using this solution for your problem.

+8
source
 '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 
0
source

By editing the KeyboardNavigationEx file from ControlzEx, I was able to solve this problem (full credit, as always, goes to punker76 ).

Just call the KeyboardHelper.Focus method by passing the UIElement that should be focused (e.g. KeyboardHelper.Focus(myCheckBox) )

Here is 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; } })); } } 
0
source

Source: https://habr.com/ru/post/1344866/


All Articles