How to remove the auto-focus behaviour after click the header of the selected TabItem?

Shannon Chow 21 Reputation points
2020-10-30T06:54:53.207+00:00

For illustration, I added 2 TabItems into a TabControl and make the first one selected. After start the app, if I click the Header of this selected TabItem, the TextBox named "txtFocused" will get focus. How to remove this default behaviour? I don't want any control in TabItem focused when click the TabItem. The txtFocused should get focus only when user click on the TextBox itself.

         <TabControl>
            <TabItem Header="123" IsSelected="True">
                <StackPanel>
                    <TextBox x:Name="txtFocused">123</TextBox>
                    <TextBox>123</TextBox>
                </StackPanel>
            </TabItem>
            <TabItem Header="123">
                <StackPanel>
                    <TextBox>223</TextBox>
                    <TextBox>432</TextBox>
                </StackPanel>
            </TabItem>
        </TabControl>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,755 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-10-30T09:49:20.803+00:00

    You can add a SelectionChanged event to TabControl , and add using System.Threading; for the class

            private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)  
            {  
                Dispatcher.BeginInvoke((ThreadStart)delegate  
                {  
                    Keyboard.ClearFocus();  
                });  
            }      
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. DaisyTian-1203 11,621 Reputation points
    2020-11-04T06:29:38.49+00:00

    You can add your TabItem header in a lable then add a click event to it, below is my code demo:
    The code for xaml:

      <Window.Resources>  
            <Style TargetType="Label" x:Key="myLable">  
                <Setter Property="Height" Value="25"></Setter>  
                <Setter Property="Width" Value="50"></Setter>  
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="Label_PreviewMouseLeftButtonDown"></EventSetter>  
            </Style>  
        </Window.Resources>  
        <Grid Name="Mygrid">  
            <TabControl Name="mytab" SelectedIndex="0">  
                <TabItem>  
                    <TabItem.Header>  
                        <Label Content="123" Style="{StaticResource myLable}"></Label>  
                    </TabItem.Header>  
                    <StackPanel>  
                        <TextBox x:Name="txtFocused">123</TextBox>  
                        <TextBox>123</TextBox>  
                    </StackPanel>  
                </TabItem>  
                <TabItem >  
                    <TabItem.Header>  
                        <Label Content="123" Style="{StaticResource myLable}"></Label>  
                    </TabItem.Header>  
                    <StackPanel>  
                        <TextBox>223</TextBox>  
                        <TextBox>432</TextBox>  
                    </StackPanel>  
                </TabItem>  
            </TabControl>  
        </Grid>  
    

    The code for Label_PreviewMouseLeftButtonDown event is same as TimonYang-MSFT.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.