maxp
maxp

Reputation: 25181

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.

Upvotes: 4

Views: 1674

Answers (2)

Yannick
Yannick

Reputation: 322

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>

@shannon it answers to your question about MVVM

Upvotes: 3

James Harcourt
James Harcourt

Reputation: 6399

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();
}

Upvotes: 2

Related Questions