views:

251

answers:

2

Is there a way to force Focus Navigation (as controlled by the Tab key or MoveFocus method) to wrap inside a given container? I have included code which demonstrates this problem below. What is the easiest way to make Tab move focus from TextBox "Charlie" to TextBox "Able" (and visa-versa for Shift+Tab on TextBox "Able") rather than moving it to MenuItem "Alpha"?

<Window x:Class="NavWrapExample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <DockPanel LastChildFill="True">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Alpha" />
            <MenuItem Header="Bravo" />
            <MenuItem Header="Charlie" />
        </Menu>
        <StackPanel>
            <TextBox Text="Able" />
            <TextBox Text="Baker" />
            <TextBox Text="Charlie" />
        </StackPanel>
    </DockPanel>
</Window>
+2  A: 

Use the KeyboardNavigation.TabNavigation attached property, like so:

<StackPanel KeyboardNavigation.TabNavigation="Cycle">
    <TextBox Text="Able" />
    <TextBox Text="Baker" />
    <TextBox Text="Charlie" />
</StackPanel>

Found the answer on Mark Smith's blog.

Joseph Sturtevant
A: 

It sounds like what you want is the same behavior as toolbars: you can tab into them, but once an element within the toolbar gets keyboard focus, focus loops inside. If so, use FocusManager as follows:

<StackPanel FocusManager.IsFocusScope="True">
    <!-- Controls go here... -->
</StackPanel>
AndyM
`IsFocusScope` is a completely different thing - it defines a new logical focus scope that remembers the logical focus within that scope, and will restore it once the keyboard ("physical") focus returns to that scope. Having it will not enable tab-looping behavior - that is controlled by `KeyboardNavigation.TabNavigation` property, as described in the other answer.
Pavel Minaev
Doh! Thanks for clarifying.
AndyM