7

I wrote a small WPF control - a textbox displaying small (i) icon allowing to display popup help. The relevant part of the control template looks like the following:

<DockPanel>
    <local:InfoIcon DockPanel.Dock="Right" Margin="2" VerticalAlignment="Center" HelpContent="{TemplateBinding InfoTooltip}" 
                    Focusable="False" IsTabStop="False"/>
    <ScrollViewer x:Name="PART_ContentHost" Margin="2" VerticalAlignment="Center"/>
</DockPanel>

The InfoIcon have IsTabStop and Focusable explicitly set to false. But that doesn't prevent that control from getting focus while tabbing through controls:

Stolen focus

How can I prevent this part of CustomControl from getting focus?

Spook
  • 25,318
  • 18
  • 90
  • 167
  • 1
    Your InfoIcon control contains child elements (the textbox you mentioned?) which still are focusable. Either set Focusable of those elements explicitly to false, or bind their Focusable property to the InfoIcon.Focusable property... (Same is applicable to IsTabStop) –  May 23 '14 at 11:33
  • have you tried this? [set KeyboardNavigation.TabNavigation="None"](http://stackoverflow.com/a/7758370/352101) – Bolu May 23 '14 at 11:35
  • @elgonzo There was a ToggleButton inside indeed. It is weird, that setting this value on parent element does not influence behaviour of child items... – Spook May 23 '14 at 11:35
  • @Bolu That's even better, it actually influences child items. – Spook May 23 '14 at 11:37
  • @Spook - it is not weird. Imagine you put a bog-standard textbox in a decorator (because you like fancy boder styles). The decorator of course should not be focusable. If *Focusable* would propagate like you believe, the textbox would basically become useless, which in turn would mean that you could not put a textbox in whatever UIElement ever :) –  May 23 '14 at 11:37
  • @Spook, regarding Bolus answer -- it does not stop the ToggleButton from obtaining focus if you click with the mouse on it -- not sure if that is what you want, though :) –  May 23 '14 at 11:39
  • 1
    @elgonzo Well, that's a good point. It seems though, that KeyboardNavigation.TabNavigation solves the problem in a way I want it to be solved. – Spook May 23 '14 at 11:39
  • @Spook yeah, that's the point, if you have multiple child items. – Bolu May 23 '14 at 11:39
  • UserControls themselves have a Focus rectangle and can be tabbed to, so setting `IsTabStop` and `Focusable` only affects the UserControl's `IsTabStop` and `Focusable` behavior. It doesn't affect the properties of any child objects. – Rachel May 27 '14 at 18:39

1 Answers1

5

Try setting KeyboardNavigation.TabNavigation="None" in the DockPanel or the InfoIcon itself.

weston
  • 54,145
  • 21
  • 145
  • 203