Search code examples
wpfcheckboxfocusborderindicator

How to make CheckBox focus border appear when calling CheckBox.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?


Solution

  • 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.