<Style TargetType="{x:Type TabItem}" x:Key="maintemplate">
<Style.Resources>
<DataTemplate x:Key="headertemplate" DataType="{x:Type TabItem}">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</StackPanel.Resources>
<ContentPresenter Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Header}"/>
<Button Content="X" x:Name="CloseButton" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TabItem}},Path=IsSelected,Converter={StaticResource BoolToVisibilityConverter}}">
<Button.LayoutTransform>
<ScaleTransform ScaleX="0.8" ScaleY="0.5"></ScaleTransform>
</Button.LayoutTransform>
</Button>
</StackPanel>
</DataTemplate>
</Style.Resources>
<Setter Property="HeaderTemplate" Value="{StaticResource headertemplate}"></Setter>
</Style>
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var isVisible = value as bool?;
if (isVisible == null || !isVisible.Value)
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}