Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to prevent a WPF custom control from drawing focus around itself while allowing child controls to get focus?

$
0
0
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 when the control receives keyboard focus, it draws 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, the user has to press Tab again to get to the first TextBox.

Setting Focusable to false on the custom control (using a setter in the style) theoretically solves the problem, but such a solution is far from perfect, because a developer using that control could set Focusable to true which would ruin the tab stop behavior.  The developer using my custom control shouldn't care about its internal structure and if he sets Focusable to True on the control, all child TextBoxes should become focusable and when he sets it to False, all child TextBoxes should become non-focusable.

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?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>