<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Actions="clr-namespace:MahApps.Metro.Actions"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters"
                    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TabControl.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Converters:MetroTabItemCloseButtonWidthConverter x:Key="MetroTabItemCloseButtonWidthConverter" />

    <Style BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type Controls:MetroTabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:MetroTabItem}">
                    <Border x:Name="Border"
                            HorizontalAlignment="Stretch"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                        <Grid HorizontalAlignment="Stretch"
                              VerticalAlignment="Stretch"
                              UseLayoutRounding="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="PART_ContentLeftCol" Width="Auto" />
                                <ColumnDefinition x:Name="PART_ContentRightCol" Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition x:Name="PART_ContentTopRow" Height="Auto" />
                                <RowDefinition x:Name="PART_ContentBottomRow" Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid x:Name="PART_ContentSite"
                                  Grid.Row="0"
                                  Grid.Column="0"
                                  UseLayoutRounding="False">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Controls:ContentControlEx x:Name="ContentSite"
                                                           Grid.Column="0"
                                                           Padding="{TemplateBinding Padding}"
                                                           Content="{TemplateBinding Header}"
                                                           ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                                           ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                           ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                           ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                                           FontFamily="{TemplateBinding FontFamily}"
                                                           FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
                                                           FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
                                                           FontStyle="{TemplateBinding FontStyle}"
                                                           FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
                                                           Foreground="{TemplateBinding Foreground}"
                                                           RecognizesAccessKey="True"
                                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                <Button x:Name="PART_CloseButton"
                                        Grid.Column="1"
                                        Width="{TemplateBinding Controls:ControlsHelper.HeaderFontSize, Converter={StaticResource MetroTabItemCloseButtonWidthConverter}}"
                                        Height="{Binding RelativeSource={RelativeSource Self}, Path=Width, Mode=OneWay}"
                                        Margin="{TemplateBinding CloseButtonMargin}"
                                        VerticalAlignment="Top"
                                        Background="{DynamicResource GrayNormalBrush}"
                                        IsTabStop="False"
                                        Style="{DynamicResource ChromelessButtonStyle}"
                                        Visibility="Collapsed">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <Actions:CloseTabItemAction Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CloseTabCommand}" CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CloseTabCommandParameter}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <Button.Resources>
                                        <Canvas x:Key="tabitem_close"
                                                Width="76"
                                                Height="76"
                                                Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                                            <Path Canvas.Left="22.1666"
                                                  Canvas.Top="22.1667"
                                                  Width="31.6666"
                                                  Height="31.6667"
                                                  Data="F1 M 26.9166,22.1667L 37.9999,33.25L 49.0832,22.1668L 53.8332,26.9168L 42.7499,38L 53.8332,49.0834L 49.0833,53.8334L 37.9999,42.75L 26.9166,53.8334L 22.1666,49.0833L 33.25,38L 22.1667,26.9167L 26.9166,22.1667 Z "
                                                  Fill="{DynamicResource BlackBrush}"
                                                  Stretch="Fill" />
                                        </Canvas>
                                    </Button.Resources>
                                    <Button.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{StaticResource tabitem_close}" />
                                    </Button.OpacityMask>
                                </Button>
                            </Grid>
                            <Controls:Underline x:Name="Underline"
                                                Grid.Row="1"
                                                Grid.Column="0"
                                                HorizontalAlignment="Stretch"
                                                VerticalAlignment="Stretch"
                                                Background="{TemplateBinding Background}"
                                                BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TabControlHelper.UnderlineBrush), Mode=OneWay}"
                                                LineExtent="3"
                                                LineThickness="2"
                                                Placement="Bottom"
                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="CloseButtonEnabled" Value="True">
                            <Setter TargetName="PART_CloseButton" Property="Visibility" Value="Hidden" />
                        </Trigger>

                        <Trigger Property="TabStripPlacement" Value="Left">
                            <Setter TargetName="PART_ContentBottomRow" Property="Height" Value="Auto" />
                            <Setter TargetName="PART_ContentLeftCol" Property="Width" Value="*" />
                            <Setter TargetName="PART_ContentRightCol" Property="Width" Value="Auto" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Column" Value="0" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Row" Value="0" />
                            <Setter TargetName="PART_ContentTopRow" Property="Height" Value="*" />
                            <Setter TargetName="Underline" Property="Grid.Column" Value="1" />
                            <Setter TargetName="Underline" Property="Grid.Row" Value="0" />
                            <Setter TargetName="Underline" Property="Placement" Value="Right" />
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Top">
                            <Setter TargetName="PART_ContentBottomRow" Property="Height" Value="Auto" />
                            <Setter TargetName="PART_ContentLeftCol" Property="Width" Value="*" />
                            <Setter TargetName="PART_ContentRightCol" Property="Width" Value="Auto" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Column" Value="0" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Row" Value="0" />
                            <Setter TargetName="PART_ContentTopRow" Property="Height" Value="*" />
                            <Setter TargetName="Underline" Property="Grid.Column" Value="0" />
                            <Setter TargetName="Underline" Property="Grid.Row" Value="1" />
                            <Setter TargetName="Underline" Property="Placement" Value="Bottom" />
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Right">
                            <Setter TargetName="PART_ContentBottomRow" Property="Height" Value="Auto" />
                            <Setter TargetName="PART_ContentLeftCol" Property="Width" Value="Auto" />
                            <Setter TargetName="PART_ContentRightCol" Property="Width" Value="*" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Column" Value="1" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Row" Value="0" />
                            <Setter TargetName="PART_ContentTopRow" Property="Height" Value="*" />
                            <Setter TargetName="Underline" Property="Grid.Column" Value="0" />
                            <Setter TargetName="Underline" Property="Grid.Row" Value="0" />
                            <Setter TargetName="Underline" Property="Placement" Value="Left" />
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Bottom">
                            <Setter TargetName="PART_ContentBottomRow" Property="Height" Value="*" />
                            <Setter TargetName="PART_ContentLeftCol" Property="Width" Value="*" />
                            <Setter TargetName="PART_ContentRightCol" Property="Width" Value="Auto" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Column" Value="0" />
                            <Setter TargetName="PART_ContentSite" Property="Grid.Row" Value="1" />
                            <Setter TargetName="PART_ContentTopRow" Property="Height" Value="Auto" />
                            <Setter TargetName="Underline" Property="Grid.Column" Value="0" />
                            <Setter TargetName="Underline" Property="Grid.Row" Value="0" />
                            <Setter TargetName="Underline" Property="Placement" Value="Top" />
                        </Trigger>

                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="None">
                            <Setter TargetName="Underline" Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="SelectedTabItem">
                            <Setter TargetName="Underline" Property="LineThickness" Value="0" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="TabPanel">
                            <Setter TargetName="Underline" Property="LineExtent" Value="4" />
                            <Setter TargetName="Underline" Property="LineThickness" Value="1" />
                        </DataTrigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="True" />
                                <Condition Property="CloseButtonEnabled" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PART_CloseButton" Property="Visibility" Value="Visible" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="CloseButtonEnabled" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PART_CloseButton" Property="Visibility" Value="Visible" />
                        </MultiTrigger>

                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource AccentColorBrush}" />
                            <Setter TargetName="Underline" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TabControlHelper.UnderlineSelectedBrush), Mode=OneWay}" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="false">
                            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource GrayNormalBrush}" />
                        </Trigger>

                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="TabPanel" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter TargetName="Underline" Property="LineThickness" Value="3" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="SelectedTabItem" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter TargetName="Underline" Property="LineThickness" Value="2" />
                        </MultiDataTrigger>

                        <Trigger SourceName="Border" Property="IsMouseOver" Value="True">
                            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource GrayHoverBrush}" />
                            <Setter TargetName="Underline" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TabControlHelper.UnderlineMouseOverBrush), Mode=OneWay}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="Border" Property="IsMouseOver" Value="True" />
                                <Condition SourceName="PART_CloseButton" Property="IsEnabled" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PART_CloseButton" Property="Background" Value="{DynamicResource GrayHoverBrush}" />
                        </MultiTrigger>
                        <Trigger SourceName="PART_CloseButton" Property="IsMouseOver" Value="True">
                            <Setter TargetName="PART_CloseButton" Property="Background" Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>

                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="TabPanel" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False" />
                                <Condition Binding="{Binding ElementName=Border, Path=IsMouseOver}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter TargetName="Underline" Property="LineThickness" Value="2" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=(Controls:TabControlHelper.Underlined)}" Value="SelectedTabItem" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False" />
                                <Condition Binding="{Binding ElementName=Border, Path=IsMouseOver}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter TargetName="Underline" Property="LineThickness" Value="2" />
                        </MultiDataTrigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="ContentSite" Property="IsMouseOver" Value="True" />
                                <Condition Property="IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource HighlightBrush}" />
                            <Setter TargetName="Underline" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TabControlHelper.UnderlineMouseOverSelectedBrush), Mode=OneWay}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>