Checkbox loses focus using FocusManager.IsFocusScope = "True"

I see strange behavior with my CheckBox and its focus / tab orientation.

First, create a β€œworking” code:

 <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Button Grid.Row="0" Width="100" Height="25"/> <TabControl Grid.Row="1" > <!--TabItem Header="tabItem1" Name="tabItem1"--> <TabItem Header="tabItem1" Name="tabItem1" FocusManager.IsFocusScope="True"> <ScrollViewer> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBox Grid.Row="0" /> <TextBox Grid.Row="1"/> <CheckBox Grid.Row="2" Content="Test" /> <TextBox Grid.Row="3"/> </Grid> </ScrollViewer> </TabItem> </TabControl> </Grid> 

If you try this, the order of the tabs will work fine - until you check CheckBox. If I check CheckBox, it loses focus, and the next tab presses the focus on the button.

If I FocusManager.IsFocusScope="True" , everything works fine.

My question is, is this a wish or a mistake?

+4
source share
1 answer

This behavior is somehow expected. To fix this, you can add a handler for GotFocus in the window.

Assuming your checkbox is called chkBox, something like this:

 protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) { base.OnGotKeyboardFocus(e); if (e.Source == chkBox) FocusManager.SetFocusedElement(this, chkBox); } 

This problem and some similar ones are described in more detail in this msdn stream .

+4
source

All Articles