简体   繁体   中英

TabItem OnSelectionChanged() setting focus on inner control (WPF)

I have two TabItem 's contained inside a TabControl .

Each TabItem contains serveral TextBox 's.

When TabControl 's OnSelectionChanged event is fired, as well as selecting the new TabItem , it is also setting focus on the first TextBox contained inside the newly selected item.

Is there any way to prevent this from happening?

Setting IsTabStop="False" on the TextBox will achieve this, but unfortunately also prevents the TextBox from being 'tabbed' into.

In your tab control, handle the focus event for each of the tabs like this:

<TabItem GotFocus="TabItem_OnGotFocus">

Then just remove focus using:

private void TabItem_OnGotFocus(object sender, RoutedEventArgs e)
{
    Keyboard.ClearFocus();
}

Just add a container to your content as Grid, Stackpanel, Border, etc. and set it Focusable. When Tab selection change, the focus is set to the container and you can also use the tab key.

<TabItem Header="myHeader">
    <StackPanel Focusable="True">
       ...    
    </StackPanel>
</TabItem>

Then it's answer the question of Shannon May with MVVM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM