Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle TabItem single click event in WPF?

Tags:

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

like image 446
pvc Avatar asked Apr 20 '11 08:04

pvc


People also ask

How to distinguish between single click and double click in WPF?

If you try to distinguish between single and double clicks of a control in WPF by hooking up event handlers for both the PreviewMouseLeftButtonDown and MouseDoubleClick events like this, you will notice that the MouseDoubleClick event handler won't be invoked when you double-click on the control: MessageBox.Show ("Single Click!");

How to handle events in WPF?

The preceding is the simplest way to handle the events in WPF and is a shorthand way, because you leave everything to the context of the object itself. You skip one more step for casting the object, you also don't need to create different functions with different appended names such as “ myButton_Click ”, “ myButton_MouseEnter ”.

How to detect double-click on previewmouseleftbuttondown?

You could handle the PreviewMouseLeftButtonDown event and check the value of the ClickCount property of the MouseButtonEventArgs. If it equals to 2, you have detected a double-click and stop (reset) the timer. If the ClickCount is less than 2, you simply start the timer and wait for the Tick event to be raised.

How do I bring another tab to the front without mouse clicking?

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). Show activity on this post. Wrap the header in a no-template button.


1 Answers

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.

like image 51
d.moncada Avatar answered Sep 17 '22 17:09

d.moncada