pvc
pvc

Reputation: 221

how to handle TabItem single click event in WPF?

In my application I have used WPF TabControl I want to handle click event of the TabItem. How do i achieve it?

Upvotes: 22

Views: 49087

Answers (7)

Plunder Bunny
Plunder Bunny

Reputation: 136

Note that, although the answer provided by d.moncada and others addresses the question, the question itself might not be what is intended. Knowing when the user clicks on a tab is different from knowing when the user makes a tab frontmost - it can be done by clicking on a tab, but it can also be achieved by other means. For example, if you click in the tab that is already frontmost, then use the left/right arrows, you can bring another tab to the front without mouse clicking - the mouse-left-button-down event does not get called in this case (as you would expect).

Upvotes: 2

Christopher Campos
Christopher Campos

Reputation: 11

You can do this by adding labels to the header property for each tabitem in the tabcontrol. Then you can set an event for the label.

The solution works pretty well; however, the "label" has margin properties which can prevent the "MouseLeftButtonDown" handler from being fired unless the user clicks the label dead on. Additionally, it looks like the styling for the rest of the tabs are affected due to label padding.

You could alleviate this by overriding the default margin/padding properties of the label...or even more simply, using TextBlock.

<TabItem x:Name="tabItem1" >
   <TabItem.Header>
      <TextBlock MouseLeftButtonDown="tabItem1_Click">
         Click Me
      </TextBlock>
   </TabItem.Header>
   ...
</TabItem>

Upvotes: 1

d.moncada
d.moncada

Reputation: 17402

You can do this by adding labels to the header property for each tabitem in the tabcontrol. Then you can set an event for the label.

xaml

<TabControl Height="100" HorizontalAlignment="Left" Name="tabControl1">
    <TabItem  Name="tabItem1">
        <TabItem.Header>
            <Label Content="tabItem1" 
                MouseLeftButtonDown="tabItem1_Clicked" 
                HorizontalAlignment="Stretch"/>
        </TabItem.Header>
        <Grid />
    </TabItem>
    <TabItem  Name="tabItem2">
        <TabItem.Header>
            <Label Content="tabItem2" 
                MouseLeftButtonDown="tabItem2_Clicked" 
                HorizontalAlignment="Stretch"/>
        </TabItem.Header>
        <Grid />
    </TabItem>
</TabControl>

C# / Code Behind

private void tabItem1_Clicked(object sender, MouseButtonEventArgs e)
{
    //DO SOMETHING
}

private void tabItem2_Clicked(object sender, MouseButtonEventArgs e)
{
    //DO SOMETHING
}

Hope this helps.

Upvotes: 24

bAN
bAN

Reputation: 13825

This is an old question but I find an answer after being in the same situation.

I use SelectionChanged event on the TabControl (XAML)

<TabControl SelectionChanged="TabControl_SelectionChanged">

Code behind (C#):

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Stuff
}

This is not the click HimSelf but its working for refreshing stuff..

Upvotes: 14

Majid gharaei
Majid gharaei

Reputation: 291

You can use SelectionChanged event in TabControl and use switch case to do anything you like.

// XAML Code

    <TabControl SelectionChanged="TabControl_SelectionChanged">
        <TabItem Header="Item1"></TabItem>
        <TabItem Header="Item2"></TabItem>
        <TabItem Header="Item3"></TabItem>
    </TabControl>

// Behind Code

    private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string tabItem = ((sender as TabControl).SelectedItem as TabItem).Header as string;

        switch(tabItem)
        {
            case "Item1":
                break;

            case "Item2":
                break;

            case "Item3":
                break;

            default:
                return;
        }
    }

Upvotes: 13

hbk
hbk

Reputation: 11184

try to find solution with GotFocus event.

private void addTabPage_GotFocus(object sender, RoutedEventArgs e)
    {
        addTabPage(); //method for adding page
        TabControlPages.SelectedIndex = TabControlPages.Items.Count - 1; //select added page
        TabControlPages.Focus(); //change forcus to selected page
    }

also method for adding page (just example)

    private void addTabPage()
    {
        TabItem tc = new TabItem();
        tc.Header = "New page";
        TabControlPages.Items.Insert(TabControlPages.Items.Count - 1, tc); //insert new page            
    }

hope this will be helpfull

Upvotes: -2

brunnerh
brunnerh

Reputation: 184441

Wrap the header in a no-template button.

If you use the ItemsSource:

<TabControl ItemsSource="{Binding Data}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <Button Click="Tab_Click">
                <Button.Template>
                    <ControlTemplate>
                        <ContentPresenter />
                    </ControlTemplate>
                </Button.Template>
                <Button.Content>
                    <!-- Actual header goes here -->
                </Button.Content>
            </Button>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

If you have static content you can insert it into the header right away:

<TabControl>
    <TabItem>
        <TabItem.Header>
            <Button Click="Tab_Click">
                <Button.Template>
                    <ControlTemplate>
                        <ContentPresenter />
                    </ControlTemplate>
                </Button.Template>
                <Button.Content>
                    <!-- Actual header goes here -->
                </Button.Content>
            </Button>
        </TabItem.Header>
    </TabItem>
</TabControl>

Upvotes: 1

Related Questions