In my application I have used WPF TabControl
I want to handle click event of the TabItem
.
How do i achieve it?
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!");
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 ”.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With