<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Fluent="clr-namespace:Fluent"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    mc:Ignorable="d">
    <ControlTemplate TargetType="{x:Type Fluent:RibbonTabItem}"
                     x:Key="RibbonTabItemControlTemplate">
        <Grid x:Name="grid"              
              Height="Auto">
            <Border x:Name="groupBorder"
                    Margin="0,0,0,1"
                    BorderThickness="0"
                    Background="{Binding Group.Background, RelativeSource={RelativeSource TemplatedParent}, FallbackValue=Transparent}"                        
                    OpacityMask="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.Contextual.Background.OpacityMask}" />
            <Border x:Name="selectedBorder"                        
                    Visibility="Collapsed"
                    VerticalAlignment="Stretch"
                    BorderBrush="{TemplateBinding ActiveTabBorderBrush}"
                    BorderThickness="1,1,1,0"                        
                    Background="{TemplateBinding ActiveTabBackground}" />
            <Border x:Name="mouseOverBorder"            
                    BorderThickness="0"
                    Background="{TemplateBinding Background}" />
            <Border x:Name="PART_ContentContainer"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch"
                    Padding="{TemplateBinding HeaderPadding}">
                <ContentControl x:Name="PART_HeaderContentHost"
                                Focusable="False"
                                AutomationProperties.Name="{TemplateBinding Header}"
                                AutomationProperties.AutomationId="{TemplateBinding Name}"
                                Content="{TemplateBinding Header}"
                                ContentTemplate="{TemplateBinding HeaderTemplate}" />
            </Border>
            <Grid x:Name="separatorGrid"
                  Margin="0"
                  Visibility="Collapsed"
                  HorizontalAlignment="Right"
                  Width="1">
                <Rectangle x:Name="separatorRectangle1"
                           Fill="{TemplateBinding BorderBrush}"
                           Stroke="{TemplateBinding BorderBrush}"
                           Margin="0"
                           Width="1">
                </Rectangle>
            </Grid>
        </Grid>

        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled"
                     Value="False">
                <Setter Property="Opacity"
                        TargetName="PART_ContentContainer"
                        Value="0.5" />
            </Trigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Fluent:RibbonTabControl}, Path=SelectedItem}"
                         Value="{x:Null}">
                <Setter TargetName="groupBorder"
                        Property="Margin"
                        Value="0" />
            </DataTrigger>
            <Trigger Property="IsSeparatorVisible"
                     Value="True">
                <Setter Property="Visibility"
                        TargetName="separatorGrid"
                        Value="Visible" />
                <Setter Property="Margin"
                        TargetName="selectedBorder"
                        Value="0,0,1,0" />
            </Trigger>
            <Trigger Property="Group"
                     Value="{x:Null}">
                <Setter Property="Visibility"
                        TargetName="groupBorder"
                        Value="Collapsed" />
            </Trigger>
            <Trigger Property="Selector.IsSelected"
                     Value="True">
                <Setter Property="Visibility"
                        TargetName="selectedBorder"
                        Value="Visible" />
            </Trigger>
            <!-- Foreground for Selected, with group -->
            <Trigger Property="Selector.IsSelected"
                     Value="True">
                <Setter Property="Foreground"
                        Value="{Binding Group.TabItemSelectedForeground, RelativeSource={RelativeSource Self}}" />
            </Trigger>
            <!-- Foreground for selected, no group -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelected"
                               Value="True" />
                    <Condition Property="Group"
                               Value="{x:Null}" />
                </MultiTrigger.Conditions>
                <Setter Property="Foreground"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.Selected.Foreground}" />
            </MultiTrigger>
            <!-- Background and foreground for MouseOver, with group -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver"
                               SourceName="grid"
                               Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        Value="{Binding Path=(Fluent:RibbonProperties.MouseOverBackground), RelativeSource={RelativeSource Self}}" />
                <Setter Property="Foreground"
                        Value="{Binding Group.TabItemMouseOverForeground, RelativeSource={RelativeSource Self}}" />
            </MultiTrigger>
            <!-- Foreground for selected MouseOver, with group -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver"
                               SourceName="grid"
                               Value="True" />
                    <Condition Property="Selector.IsSelected"
                               Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Foreground"
                        Value="{Binding Group.TabItemSelectedMouseOverForeground, RelativeSource={RelativeSource Self}}" />
            </MultiTrigger>
            <!-- Background and foreground for MouseOver, no group -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver"
                               SourceName="grid"
                               Value="True" />
                    <Condition Property="Group"
                               Value="{x:Null}" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        Value="{Binding Path=(Fluent:RibbonProperties.MouseOverBackground), RelativeSource={RelativeSource Self}}" />
                <Setter Property="Foreground"
                        Value="{Binding Path=(Fluent:RibbonProperties.MouseOverForeground), RelativeSource={RelativeSource Self}}" />
            </MultiTrigger>
            <!-- Background for Selected, Minimized, IsOpen, with group -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelected"
                               Value="True" />
                    <Condition Property="IsMinimized"
                               Value="True" />
                    <Condition Property="IsOpen"
                               Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        TargetName="grid"
                        Value="Transparent" />
            </MultiTrigger>
            <!-- Background for Selected, Minimized, IsOpen, no group -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelected"
                               Value="True" />
                    <Condition Property="IsMinimized"
                               Value="True" />
                    <Condition Property="IsOpen"
                               Value="True" />
                    <Condition Property="Group"
                               Value="{x:Null}" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        TargetName="grid"
                        Value="Transparent" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="RibbonTabItemStyle"
           TargetType="{x:Type Fluent:RibbonTabItem}">
        <Setter Property="FocusVisualStyle"
                Value="{DynamicResource ControlStyleEmptyFocus}" />        
        <Setter Property="HorizontalAlignment"
                Value="Left" />
        <Setter Property="VerticalAlignment"
                Value="Stretch" />
        <Setter Property="Height"
                Value="Auto" />
        <Setter Property="Margin"
                Value="0" />
        <Setter Property="IsMinimized"
                Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:RibbonTabControl}}, Path=IsMinimized, Mode=OneWay, FallbackValue=False}" />
        <Setter Property="IsOpen"
                Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:RibbonTabControl}}, Path=IsDropDownOpen, Mode=OneWay, FallbackValue=True}" />
        <Setter Property="Template"
                Value="{DynamicResource RibbonTabItemControlTemplate}" />
        <Setter Property="Foreground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.Foreground}" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.BorderBrush}" />
        <Setter Property="ActiveTabBackground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.Active.Background}" />
        <Setter Property="ActiveTabBorderBrush"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.BorderBrush}" />
        <Setter Property="Fluent:RibbonProperties.MouseOverBackground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.MouseOver.Background}" />
        <Setter Property="Fluent:RibbonProperties.MouseOverForeground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.MouseOver.Foreground}" />
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock VerticalAlignment="Center"
                               HorizontalAlignment="Center"
                               Text="{Binding}"
                               TextWrapping="NoWrap"
                               TextTrimming="CharacterEllipsis" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Group"
                     Value="{x:Null}">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.BorderBrush}" />
            </Trigger>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="Fluent:RibbonProperties.MouseOverForeground"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.Selected.MouseOver.Foreground}" />
            </Trigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding IsContextual, RelativeSource={RelativeSource Self}}"
                               Value="True" />
                    <Condition Binding="{Binding Group.Visibility, RelativeSource={RelativeSource Self}}"
                               Value="Collapsed" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Visibility"
                            Value="Collapsed" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>