Remove focus rectangle on a UserControl

WpfFocusFocusvisualstyle

Wpf Problem Overview


I have a WPF UserControl with Focusable="True". It's the only focusable control in the window.

Whenever the user presses Tab or Alt (and especially when they Alt+Tab to another application), my UserControl acquires a dotted-line border, aka focus rectangle. The focus rectangle then stays there until the window is closed.

How can I prevent my UserControl from ever displaying this focus rectangle?

Edit

It turns out the focus rectangle wasn't actually being displayed by my UserControl. My Focusable UserControl contained another UserControl that, in turn, contained an ItemsControl, and the ItemsControl is what was showing the focus rectangle.

When I added FocusVisualStyle="{x:Null}" to the ItemsControl, the focus rectangle went away.

Wpf Solutions


Solution 1 - Wpf

If you want to not display the focus rectangle in any case you could set the FocusVisualStyle to null.

<MyControl FocusVisualStyle="{x:Null}" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoe WhiteView Question on Stackoverflow
Solution 1 - WpfSacha BruttinView Answer on Stackoverflow