<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:converters="clr-namespace:Fluent.Converters">

    <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
    <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />

    <ControlTemplate x:Key="RibbonTabControlToggleButtonControlTemplate"
                     TargetType="{x:Type Fluent:ToggleButton}">
        <Border x:Name="border"
                BorderThickness="1"
                Background="Transparent"
                BorderBrush="Transparent"
                Height="22"
                HorizontalAlignment="Left"
                VerticalAlignment="Stretch"
                Width="22">
            <ContentPresenter x:Name="iconImage"
                              Width="16"
                              Height="16"
                              Margin="2"
                              HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Content="{converters:ObjectToImageConverter {Binding Icon, RelativeSource={RelativeSource TemplatedParent}}, '16,16', {Binding RelativeSource={RelativeSource TemplatedParent}}}"                                                            
                              SnapsToDevicePixels="True" />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed"
                     Value="True">
                <Setter Property="BorderBrush"
                        TargetName="border"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.Pressed.BorderBrush}" />
                <Setter Property="Background"
                        TargetName="border"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.Pressed.Background}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver"
                               Value="True" />
                    <Condition Property="IsPressed"
                               Value="False" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        TargetName="border"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
                <Setter Property="BorderBrush"
                        TargetName="border"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.BorderBrush}" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="RibbonTabControlToggleButtonStyle"
           TargetType="{x:Type Fluent:ToggleButton}">
        <Setter Property="Template"
                Value="{DynamicResource RibbonTabControlToggleButtonControlTemplate}" />
        <Setter Property="Height"
                Value="22" />
        <Setter Property="Width"
                Value="22" />
        <Setter Property="Foreground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.LabelTextBrush}" />
        <Setter Property="CanAddToQuickAccessToolBar"
                Value="False" />
    </Style>

    <Style x:Key="RibbonTabControlStyle"
           TargetType="{x:Type Fluent:RibbonTabControl}">
        <Setter Property="VerticalAlignment"
                Value="Top" />
        <Setter Property="Background"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Background}" />
        <Setter Property="Foreground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Foreground}" />
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="Template"
                Value="{DynamicResource RibbonTabControlControlTemplate}" />
        <Setter Property="CanMinimize"
                Value="{Binding CanMinimize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:Ribbon}}, Mode=TwoWay}" />
        <Setter Property="IsMinimized"
                Value="{Binding IsMinimized, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:Ribbon}}, Mode=TwoWay}" />
        <Setter Property="ContentGapHeight"
                Value="{Binding ContentGapHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:Ribbon}}}" />
    </Style>

    <Fluent:ScreenTip x:Key="Fluent.Ribbon.Resources.RibbonTabControl.MinimizeScreenTip"
                      x:Shared="False"
                      Title="{Binding Source={x:Static Fluent:RibbonLocalization.Current}, Path=Localization.MinimizeButtonScreenTipTitle, Mode=OneWay}"
                      Text="{Binding Source={x:Static Fluent:RibbonLocalization.Current}, Path=Localization.MinimizeButtonScreenTipText, Mode=OneWay}" />

    <Fluent:ScreenTip x:Key="Fluent.Ribbon.Resources.RibbonTabControl.ExpandScreenTip"
                      x:Shared="False"
                      Title="{Binding Source={x:Static Fluent:RibbonLocalization.Current}, Path=Localization.ExpandButtonScreenTipTitle, Mode=OneWay}"
                      Text="{Binding Source={x:Static Fluent:RibbonLocalization.Current}, Path=Localization.ExpandButtonScreenTipText, Mode=OneWay}" />

    <ControlTemplate x:Key="RibbonTabControlControlTemplate"
                     TargetType="{x:Type Fluent:RibbonTabControl}">
        <ControlTemplate.Resources>
            <Style TargetType="{x:Type ScrollViewer}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ScrollViewer}">
                            <Grid x:Name="Grid"
                                  Background="{TemplateBinding Background}"
                                  IsHitTestVisible="True">
                                <ScrollContentPresenter  Margin="{TemplateBinding Padding}"
                                                         x:Name="PART_ScrollContentPresenter"
                                                         Content="{TemplateBinding Content}"
                                                         ContentTemplate="{TemplateBinding ContentTemplate}"
                                                         CanContentScroll="{TemplateBinding CanContentScroll}"
                                                         IsHitTestVisible="True" />
                                <RepeatButton Command="{x:Static ScrollBar.LineLeftCommand}"
                                              CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                              Focusable="false"
                                              HorizontalAlignment="Left"
                                              Width="13"
                                              Height="Auto"
                                              VerticalAlignment="Stretch"
                                              Style="{DynamicResource ScrollButtonStyle}">
                                    <RepeatButton.Visibility>
                                        <MultiBinding FallbackValue="Visibility.Collapsed"
                                                      Converter="{StaticResource MenuScrollingVisibilityConverter}"
                                                      ConverterParameter="0">
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="ComputedHorizontalScrollBarVisibility" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="HorizontalOffset" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="ExtentWidth" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="ViewportWidth" />
                                        </MultiBinding>
                                    </RepeatButton.Visibility>
                                    <Path Fill="{DynamicResource BlackBrush}"
                                          Stretch="Fill"
                                          Stroke="{x:Null}"
                                          StrokeThickness="0"
                                          HorizontalAlignment="Center"
                                          Margin="0"
                                          VerticalAlignment="Center"
                                          Width="3"
                                          Height="5"
                                          Data="M0,0L1,1 1,-1z" />
                                </RepeatButton>
                                <RepeatButton Command="{x:Static ScrollBar.LineRightCommand}"
                                              CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                              Focusable="false"
                                              HorizontalAlignment="Right"
                                              Width="13"
                                              Height="Auto"
                                              VerticalAlignment="Stretch"
                                              Style="{DynamicResource ScrollButtonStyle}">
                                    <RepeatButton.Visibility>
                                        <MultiBinding FallbackValue="Visibility.Collapsed"
                                                      Converter="{StaticResource MenuScrollingVisibilityConverter}"
                                                      ConverterParameter="100">
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="ComputedHorizontalScrollBarVisibility" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="HorizontalOffset" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="ExtentWidth" />
                                            <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                     Path="ViewportWidth" />
                                        </MultiBinding>
                                    </RepeatButton.Visibility>
                                    <Path Fill="{DynamicResource BlackBrush}"
                                          Stretch="Fill"
                                          Stroke="{x:Null}"
                                          StrokeThickness="0"
                                          HorizontalAlignment="Center"
                                          Margin="0"
                                          VerticalAlignment="Center"
                                          Width="3"
                                          Height="5"
                                          Data="M1,0L0,1 0,-1z" />
                                </RepeatButton>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="CanContentScroll"
                        Value="True" />
                <Setter Property="HorizontalScrollBarVisibility"
                        Value="Auto" />
                <Setter Property="VerticalScrollBarVisibility"
                        Value="Disabled" />
                <Setter Property="PanningMode"
                        Value="HorizontalOnly" />
            </Style>
        </ControlTemplate.Resources>
        <Grid x:Name="ContentGrid"
              Background="{TemplateBinding Background}"
              KeyboardNavigation.TabNavigation="Local"
              Height="Auto"
              VerticalAlignment="Top">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition x:Name="rowDefinition"
                               MinHeight="{TemplateBinding ContentHeight}"
                               MaxHeight="{TemplateBinding ContentHeight}" />
                <RowDefinition x:Name="rowDefinition1"
                               MinHeight="{TemplateBinding ContentGapHeight}"
                               MaxHeight="{TemplateBinding ContentGapHeight}" />
            </Grid.RowDefinitions>
            <Border x:Name="border"
                    HorizontalAlignment="Stretch"
                    Margin="0,-1,0,0"
                    Width="Auto"
                    BorderThickness="0,1"
                    Grid.Row="1"
                    BorderBrush="{Binding Path=SelectedItem.BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
            
            <Border HorizontalAlignment="Stretch"
                    Grid.Row="1"
                    Grid.RowSpan="1"
                    Background="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Content.Background}"
                    BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Content.BorderBrush}"
                    BorderThickness="{DynamicResource Fluent.Ribbon.Values.RibbonTabControl.Content.BorderThickness}"
                    TextElement.Foreground="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Content.Foreground}"
                    KeyboardNavigation.TabNavigation="Local"
                    KeyboardNavigation.DirectionalNavigation="Contained"
                    KeyboardNavigation.TabIndex="2">
                <ContentControl x:Name="mainContentControl"
                                Margin="0"
                                Focusable="False">
                    <ContentPresenter ContentSource="SelectedContent"
                                      x:Name="PART_SelectedContentPresenter"
                                      HorizontalAlignment="Stretch"
                                      Margin="0" />
                </ContentControl>
            </Border>
            
            <Border Margin="0"
                    VerticalAlignment="Stretch"                    
                    Grid.Row="2"
                    Background="{Binding Path=SelectedItem.BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
            
            <Grid x:Name="TabsGrid"
                  Background="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.TabsGrid.Background}"
                  TextElement.Foreground="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.TabsGrid.Foreground}"
                  HorizontalAlignment="Stretch"
                  Width="Auto"
                  Margin="0"
                  Grid.Row="0"
                  KeyboardNavigation.TabIndex="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <Fluent:WindowSteeringHelperControl Grid.ColumnSpan="4" />

                <!-- Margin on bottom is need to keep the border surrounding the content area visible below the menu presenter -->
                <ContentPresenter Grid.Column="0"
                                  VerticalAlignment="Stretch"
                                  ContentSource="Menu"
                                  Margin="0 0 0 1" />

                <Fluent:RibbonScrollViewer Grid.Column="1"
                                           Visibility="{Binding AreTabHeadersVisible, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource boolToVisibilityConverter}}"
                                           HorizontalAlignment="Left"
                                           Margin="0"
                                           Width="Auto"
                                           Style="{DynamicResource RibbonTabScrollViewer}"
                                           HorizontalScrollBarVisibility="Auto"
                                           VerticalScrollBarVisibility="Disabled"
                                           IsHitTestVisible="True"
                                           IsTabStop="False">
                    <Fluent:RibbonTabsContainer x:Name="PART_TabsContainer"
                                                IsItemsHost="True"
                                                Height="Auto"
                                                HorizontalAlignment="Stretch"
                                                Margin="0"
                                                KeyboardNavigation.TabIndex="1" />
                </Fluent:RibbonScrollViewer>

                <StackPanel HorizontalAlignment="Right"
                            Grid.Column="3"
                            Orientation="Horizontal"
                            Margin="0,0,0,1">
                    <Fluent:ToggleButton x:Name="PART_MinimizeButton"
                                         Margin="0"
                                         Width="22"
                                         Height="22"
                                         Size="Small"
                                         Style="{DynamicResource RibbonTabControlToggleButtonStyle}"
                                         IsChecked="{Binding Path=IsMinimized, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                         Visibility="{TemplateBinding CanMinimize, Converter={StaticResource boolToVisibilityConverter}}" />

                    <StackPanel x:Name="PART_ToolbarPanel"
                                Visibility="{Binding IsToolBarVisible, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource boolToVisibilityConverter}}"
                                Orientation="Horizontal" />
                </StackPanel>
            </Grid>

            <!-- Placement target must be the selected item. Otherwise things like DPI and monitor are selected wrong if the tabcontrol 0,0 is on a different screen than the selected tabs 0,0 -->
            <Popup x:Name="PART_Popup"
                   PlacementTarget="{Binding Path=SelectedTabItem, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                   Placement="Custom"
                   AllowsTransparency="True"
                   PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
                   IsOpen="{TemplateBinding IsDropDownOpen}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="{x:Static Fluent:RibbonTabControl.AdditionalPopupSpaceForKeyTipsGridLength}" /> <!-- Needed for overlapping Keytips -->
                        <RowDefinition MinHeight="{TemplateBinding ContentHeight}"
                                       MaxHeight="{TemplateBinding ContentHeight}" />
                        <RowDefinition Height="{x:Static Fluent:RibbonTabControl.AdditionalPopupSpaceForKeyTipsGridLength}" /> <!-- Needed for overlapping Keytips -->
                    </Grid.RowDefinitions>
                    <Border Grid.Row="1" 
                            BorderThickness="0,0,0,1"
                            Margin="1,0"
                            BorderBrush="{Binding Path=SelectedItem.BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"
                            Background="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Content.Background}"
                            TextElement.Foreground="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabControl.Content.Foreground}"
                            KeyboardNavigation.TabNavigation="Local"
                            KeyboardNavigation.DirectionalNavigation="Contained">
                        <ContentControl x:Name="popupContentControl"
                                        Focusable="False"
                                        Margin="0" />
                    </Border>
                </Grid>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMinimized"
                     Value="False">
                <Setter TargetName="PART_MinimizeButton"
                        Property="Icon" 
                        Value="{DynamicResource Fluent.Ribbon.Images.RibbonCollapse}" />
                <Setter TargetName="PART_MinimizeButton" 
                        Property="ToolTip"
                        Value="{DynamicResource Fluent.Ribbon.Resources.RibbonTabControl.MinimizeScreenTip}" />
            </Trigger>
            <Trigger Property="IsMinimized"
                     Value="True">
                <Setter Property="MinHeight"
                        TargetName="rowDefinition"
                        Value="0" />
                <Setter Property="MinHeight"
                        TargetName="rowDefinition1"
                        Value="0" />
                <!-- Switch content to popup -->
                <Setter Property="Content"
                        TargetName="mainContentControl"
                        Value="{x:Null}" />
                <Setter Property="Content"
                        TargetName="popupContentControl"
                        Value="{Binding ElementName=PART_SelectedContentPresenter, Mode=OneWay}" />
                <!-- Switch content to popup END -->
                <Setter TargetName="PART_MinimizeButton" 
                        Property="Icon"
                        Value="{DynamicResource Fluent.Ribbon.Images.RibbonExpand}" />
                <Setter TargetName="PART_MinimizeButton" 
                        Property="ToolTip"
                        Value="{DynamicResource Fluent.Ribbon.Resources.RibbonTabControl.ExpandScreenTip}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsDropDownOpen"
                               Value="True" />
                    <Condition Property="IsMinimized"
                               Value="True" />
                </MultiTrigger.Conditions>
                <Setter TargetName="PART_MinimizeButton"
                        Property="Icon" 
                        Value="{DynamicResource Fluent.Ribbon.Images.RibbonPin}" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>