Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

like image 485
maxp Avatar asked Sep 19 '25 10:09

maxp


1 Answers

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

like image 60
Yannick Avatar answered Sep 21 '25 00:09

Yannick