Tab stop does not work for text box control templates when windows theme is classic - What can I do?

StackOverflow https://stackoverflow.com/questions/18760135

Frage

When having, let's say, a TextBox and a Button like below, everything's fine. You can use tab stop to go back and forth between both controls.

<StackPanel>
  <TextBox>Type Here</TextBox>
  <Button>Click Me</Button>
</StackPanel>

But if you redefine the TextBox layout you cannot use tab stop to navigate to the button.

<Style TargetType="TextBox">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel>
          <ScrollViewer x:Name="PART_ContentHost" />
          <Button>Template-Button</Button>
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

That is, you can still navigate back and forth between the text area and the 'Click Me'-button (which is still a separate control) but you won't reach the 'Template-Button' (which is part of the TextBox):

enter image description here

This issue does not occur with Windows 7 Basic style.

It seems that this is true for templating TextBox, but not necessarily to any controls. When templating a Button you can reach any parts of the control. In the following example you can reach the ComboBox as well as the TextBox.

<Style TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel>
          <ComboBox>ComboBox</ComboBox>
          <TextBox>TextBox</TextBox>
        </StackPanel>
      </ControlTemplate>
     </Setter.Value>
   </Setter>
 </Style>

I wonder why it's like that and if there's any workaround other than not using ControlTemplate and 'misuse' of another control (like Button) as I lose TextBox-features and have to re-implement them.

Unfortunately, I didn't find anything when searching the Internet. I found some sources stating that the Windows classic templates are differently implemented than the respective Aero templates (for instance the padding issue) but I'm not sure if this applies to tab stop as well (though there's a property IsTabStop - at least, I wasn't able to fix this issue by explicitly setting this property to True).

So, anything I can do?


Some additional notes:

  • It's even worse for a PasswordBox as you cannot reach any templated elements for both Windows 7 classic and basic theme.

  • This issue is reproducible on a Windows 7 system but AFAIK it also appears on Windows 8.

War es hilfreich?

Lösung

I found the answer to my question right here on another question Tab stop not working for textboxes inside data template.

I didn't find it in the first place as I focused my search on windows 7 classic style.

So, all I need to do to fix this issue is to add a setter to my style which sets the KeyboardNavigation.TabNavigation value to Continue.

<Setter Property="KeyboardNavigation.TabNavigation" Value="Continue" />

Due to this value each element gets keyboard focus:

Continue: Each element receives keyboard focus, as long as it is a navigation stop. Navigation leaves the containing element when an edge is reached.

According to MSDN the default value is Continue but this is not true for the classic theme where the default value actually is None. Unfortunately, there's no hint anywhere.

Andere Tipps

Have you tried setting the Control.IsTabStop property on your TextBox to True? Please take a look at the Control.IsTabStop Property page at MSDN for more information.

<TextBox IsTabStop="True" ... />
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top