mercredi 6 mai 2015

WPF Keyboard Focus and Tabbing

If I have a group of buttons as a wpf control, for example:

<UserControl xmlns="http://ift.tt/o66D3f"
             xmlns:x="http://ift.tt/mPTqtT">
<StackPanel>
    <Button Content="Button 1"/>
    <Button Content="Button 2"/>
</StackPanel>
</UserControl>

and run this application, Button 1, as expected, has focus on start-up, but with no FocusVisualStyle, i.e., dotted line surrounding the button. Pressing the tab key gives Button 2 focus, and gives it FocusVisualStyle. Pressing tab again gives focus back to Button 1 with FocusVisualStyle.

Here's the behaviour I'm after: On start-up, Button 1 should have focus with no FocusVisualStyle. Pressing tab keeps focus on Button 1 and gives it FocusVisualStyle (not Button 2). Pressing tab again cycles through the buttons giving each focus and FocusVisualStyle accordingly.

Ideally I want to achieve this in XAML, but not sure it is possible having tried various combinations of setting the focused element via the focus manager. Any suggestion how this might be achieved?

Aucun commentaire:

Enregistrer un commentaire