0

I have a TabControl with two TabItems, inside the one TabItem I have a DataGrid. I'm trying to handle the TabItem click, and it works, but, when I click in one row of the "dataGrid1" the event "TabItem_MouseLeftButtonUp" of TabItem click is fired too. See the code:

<TabControl Height="211" HorizontalAlignment="Left" Margin="33,29,0,0" Name="tabControl1" VerticalAlignment="Top" Width="417" >
        <TabItem Header="tabItem1" Name="tabItem1">
            <Grid />
        </TabItem>
        <TabItem MouseLeftButtonUp="TabItem_MouseLeftButtonUp">
            <DataGrid AutoGenerateColumns="True" Height="134" Name="dataGrid1" Width="307" />
        </TabItem>
</TabControl>

Note: I can't use the personalize <TabItem.Header> because I'm using MahApps, if I use TabItem.Header the style os TabItem will break.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Ewerton
  • 4,046
  • 4
  • 30
  • 56

2 Answers2

0

You will recieve the EventArgs with MouseLeftButtonUp event. Just filter out whatever you need.

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
0

The MouseLeftButtonUp event is bubbling routed event. When you on the DataGrids row the event bubbling through its ancestors and calls the corresponding handlers, TabItem_MouseLeftButtonUp for TabItem in your case.

In your TabItem_MouseLeftButtonUp event you can check who raised the event, which control is the origin. If its not the TabItem do nothing.

private void TabItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
     if(sender is TabItem)
     {
           //do the work
     }
}
Miklós Balogh
  • 2,164
  • 2
  • 19
  • 26
  • When a click on the TabItem the OriginalSource is a Border, and when i click right on the text of the TabItem the OriginalSource is a TextBlock, so, this solution dont work. – Ewerton Sep 19 '12 at 12:30
  • I do some modifications in your solution the sender.GetType() will return the type os the control (DataGrid or TabItem), so, your solution works, i will mark it as answer, can you edit it? – Ewerton Sep 19 '12 at 12:40