How to add Close Button in tab of Dynamic WPF Tab control

I have tried several solutions and struggled to find something that looked nice and also highlighted the "X" in the button when the mouse hovers. I finally ended with this one. It also does not require too much code. I hope it helps:

enter image description here

<TabControl>
        <TabItem>
            <TabItem.Header>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0">Output</TextBlock>
                    <Button Grid.Column="1" Name="button_close" Click="button_close_Click">
                  <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Path Data="M0,0 L8,8 M8,0 L0,8" StrokeThickness="3" VerticalAlignment="Center" Margin="5,4,0,2">
                                    <Path.Style>
                                        <Style TargetType="{x:Type Path}">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Stroke" Value="LightGray" />
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Stroke" Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Path.Style>
                                </Path>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </Grid>


            </TabItem.Header>
         <TabItem.Content>
         </TabItem.Content>


if you make the tab control as a custom control and inherit the functionality from tab control and then add close button and handle its events then it can work

    <UserControl x:Class="CloseableHeader"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="23" d:DesignWidth="81" Margin="0">
<Grid>
  <Button Content="X"  Height="19" HorizontalAlignment="Right" Margin="0,3,4,0" 
      Name="button_close" VerticalAlignment="Top" Width="20" FontFamily="Courier" 
      FontWeight="Bold" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
      FontStretch="Normal" Visibility="Visible" 
      FontSize="14" Padding="0" ToolTip="Close"/>
  <Label Content="TabItem"  Height="23" HorizontalAlignment="Left" 
      Margin="4,1,0,0" Name="label_TabTitle" VerticalAlignment="Top" 
      FontFamily="Courier" FontSize="12" />
</Grid>

    class ClosableTab : TabItem
     {
        // Constructor
      public ClosableTab()
      {
         // Create an instance of the usercontrol
         closableTabHeader = new CloseableHeader();
         // Assign the usercontrol to the tab header
         this.Header = closableTabHeader;
       }
    }

see in this article for details http://www.codeproject.com/Articles/84213/How-to-add-a-Close-button-to-a-WPF-TabItem this might be helpful

Tags:

C#

Wpf

Tabcontrol