WPF TabItem Element MouseClick Event
-
22-09-2019 - |
Question
In WPF app inside a TabControl
there is a TabItem
element with one Label
. I want to fire the event on MouseClick
on the Tab
of this TabControl
, but the event MouseLeftButtonDown
:
<TabItem Header="Header1" MouseLeftButtonDown="TabItem_MouseLeftButtonDown" >
<Label Height="28" Name="AdderLbl" Width="120" Background="Azure" >Label</Label>
</TabItem>
fires only on click over the label. It is not wat I want.
How I could fire the event on MouseClick
over the Tab
, not its content?
Solution
What are you trying to achieve? It looks like you're trying to trap when the user moves to the tab, if so you can hook a different event SelectionChanged
:-)
OTHER TIPS
I did not want selectionchanged event because that would involve a switch. So i wanted an event directly on the tab clicked.
While i thought an MouseDown event was what I was looking for.... after wiring up all sorts of mouse events with break points; i found the MouseUp events worked for me.
So I used MouseLeftButtonUp
or MouseUp
.
For who really want to use something like MouseLeftButtonDown event, you can use the "MouseUp" event instead. =)
While handling SelectionChanged
event is the better approach to handling tab changes, here is a possible solution to your question. I don't recommend it in your case, but it illustrates an approach that may be useful in other cases
<TabControl>
<TabItem>
<TabItem.Header>
<Button Click="Button_Click">
<Button.Template>
<ControlTemplate>
<Label>Header1</Label>
</ControlTemplate>
</Button.Template>
</Button>
</TabItem.Header>
<Label Height="28" Name="AdderLbl" Width="120" Background="Azure" >Label</Label>
</TabItem>
</TabControl>
You need the event Selector.Selected
<TabControl x:Name ="TabControls" Dock="Top" DockWidth="500" DockHeight="500" TabItemShape="Rounded">
<TabControl>
TabControls.SelectionChanged += SelectionChanged;
private void SelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs) {
var itemTab = (TabItem) TabControls.SelectedItem;
(...)
}