There is a form with tabControl:

<TabControl x:Name="tabs"> 

tabItem of the following form:

 <TabItem> <TabItem.Header> <StackPanel> <TextBlock Margin="3">Tab 1</TextBlock> <Button/> </StackPanel> </TabItem.Header> </TabItem> 

For the button in the styles indicated the following:

 <Style TargetType="Button"> <Setter Property="Content" Value ="x"/> <Setter Property="IsCancel" Value ="True"/> <EventSetter Event="Click" Handler="Button_Click"/> </Style> 

How to press the button to close the tab on which the button is located?

I tried to do this:

 private void Button_Click(object sender, RoutedEventArgs e) { var tabItem = e.Source as TabItem; tabs.Items.Remove(tabItem); } 

However, the removal of the tobacco does not occur. I found this instruction on the Internet, what am I doing wrong?

    1 answer 1

    Wrong data transfer:

     private TabItem GetTargetTabItem(object originalSource) { var current = originalSource as DependencyObject; while (current != null) { var tabItem = current as TabItem; if (tabItem != null) { return tabItem; } current = VisualTreeHelper.GetParent(current); } return null; } private void Button_Click(object sender, RoutedEventArgs e) { var tabItem = GetTargetTabItem(e.OriginalSource); tabs.Items.Remove(tabItem); }