I'm developing a custom control deriving from Control and defined using ControlTemplate. After stripping out all bells and whistles, the control displays just four TextBoxes:
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<StackPanel Orientation="Horizontal">
<TextBox/>
<TextBox/>
<TextBox/>
<TextBox/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is that the control is able to receive keyboard focus and draw the focus rectangle around itself (not one of the TextBoxes, but around all of them). This adds an unnecessary tab stop when navigating through the controls in the Window.
Setting Focusible to false on the custom control (using a setter in the style) solves the problem, but such a solution is far from perfect, because a developer using that control could set Focusible to true which would ruin the tab stop behavior.
I could respond to the control's GotKeyboardFocus event and give focus to the first TextBox when the whole control receives focus, but this will not work properly when tabbing back (using Shift+Tab), the first TextBox would receive focus while in such a case the last TextBox should be focused first.
How can I prevent the whole custom control from accepting keyboard focus, but allow child TextBoxes to act normally as tab stops?
FocusVisualStyle
tonull
? – Kent BoogaartFocusable
tofalse
too. Some other developer setting it back totrue
is their problem. Indeed, you should override the metadata forFocusableProperty
such that it defaults tofalse
for your control. – Kent Boogaart