<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ModernWpf.Controls"
    xmlns:primitives="clr-namespace:ModernWpf.Controls.Primitives">

    <primitives:CornerRadiusFilterConverter x:Key="TopLeftCornerRadiusDoubleValueConverter" Filter="TopLeftValue" />
    <primitives:CornerRadiusFilterConverter x:Key="BottomRightCornerRadiusDoubleValueConverter" Filter="BottomRightValue" />

    <Style x:Key="TabItemPivotStyle" TargetType="TabItem">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Background" Value="{DynamicResource PivotHeaderItemBackgroundUnselected}" />
        <Setter Property="Padding" Value="{DynamicResource PivotHeaderItemMargin}" />
        <Setter Property="Height" Value="48" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
        <Setter Property="primitives:FocusVisualHelper.UseSystemFocusVisuals" Value="{DynamicResource UseSystemFocusVisuals}" />
        <Setter Property="primitives:ControlHelper.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <Border
                        x:Name="Border"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="True">
                        <Grid>
                            <ContentPresenter
                                x:Name="ContentPresenter"
                                ContentSource="Header"
                                TextElement.Foreground="{DynamicResource PivotHeaderItemForegroundUnselected}"
                                TextElement.FontSize="{DynamicResource PivotHeaderItemFontSize}"
                                TextElement.FontFamily="{DynamicResource PivotHeaderItemFontFamily}"
                                TextElement.FontWeight="{DynamicResource PivotHeaderItemThemeFontWeight}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Stretch"
                                Focusable="False"
                                RecognizesAccessKey="True"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            <Rectangle
                                x:Name="SelectedPipe"
                                Fill="{DynamicResource PivotHeaderItemSelectedPipeFill}"
                                Height="2"
                                VerticalAlignment="Bottom"
                                HorizontalAlignment="Stretch"
                                Margin="0,0,0,2"
                                RadiusX="{TemplateBinding primitives:ControlHelper.CornerRadius, Converter={StaticResource TopLeftCornerRadiusDoubleValueConverter}}"
                                RadiusY="{TemplateBinding primitives:ControlHelper.CornerRadius, Converter={StaticResource BottomRightCornerRadiusDoubleValueConverter}}" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <!-- Unselected -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="False" />
                                <Condition SourceName="Border" Property="IsMouseOver" Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="SelectedPipe" Property="Visibility" Value="Collapsed" />
                        </MultiTrigger>
                        <!-- Selected -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="True" />
                                <Condition SourceName="Border" Property="IsMouseOver" Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Panel.ZIndex" Value="1" />
                            <Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource PivotHeaderItemForegroundSelected}" />
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource PivotHeaderItemBackgroundSelected}" />
                        </MultiTrigger>
                        <!-- UnselectedPointerOver -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="False" />
                                <Condition SourceName="Border" Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource PivotHeaderItemForegroundUnselectedPointerOver}" />
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource PivotHeaderItemBackgroundUnselectedPointerOver}" />
                            <Setter TargetName="SelectedPipe" Property="Visibility" Value="Collapsed" />
                        </MultiTrigger>
                        <!-- SelectedPointerOver -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="True" />
                                <Condition SourceName="Border" Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Panel.ZIndex" Value="1" />
                            <Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource PivotHeaderItemForegroundSelectedPointerOver}" />
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource PivotHeaderItemBackgroundSelectedPointerOver}" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource PivotHeaderItemForegroundDisabled}" />
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource PivotHeaderItemBackgroundDisabled}" />
                            <Setter TargetName="SelectedPipe" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="TabControlPivotStyle" TargetType="TabControl">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Background" Value="{DynamicResource PivotBackground}" />
        <Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" />
        <Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
        <Setter Property="ItemContainerStyle" Value="{StaticResource TabItemPivotStyle}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabControl">
                    <ControlTemplate.Resources>
                        <StreamGeometry x:Key="ChevronLeft">M 14.091797 19.970703 L 4.111328 10 L 14.091797 0.029297 L 14.970703 0.908203 L 5.888672 10 L 14.970703 19.091797 Z</StreamGeometry>
                        <StreamGeometry x:Key="ChevronRight">M 5.029297 19.091797 L 14.111328 10 L 5.029297 0.908203 L 5.908203 0.029297 L 15.888672 10 L 5.908203 19.970703 Z</StreamGeometry>
                    </ControlTemplate.Resources>
                    
                    <Grid
                        x:Name="templateRoot"
                        Background="{TemplateBinding Background}"
                        KeyboardNavigation.TabNavigation="Local"
                        SnapsToDevicePixels="true"
                        ClipToBounds="true">

                        <Grid.Resources>
                            <Style x:Key="BaseContentControlStyle" TargetType="ContentControl">
                                <Setter Property="HorizontalContentAlignment" Value="Left" />
                                <Setter Property="VerticalContentAlignment" Value="Top" />
                                <Setter Property="FontFamily" Value="{DynamicResource {x:Static SystemFonts.MessageFontFamilyKey}}" />
                                <Setter Property="FontWeight" Value="SemiBold" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ContentControl">
                                            <ContentPresenter
                                                Content="{TemplateBinding Content}"
                                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                                Margin="{TemplateBinding Padding}"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                            <Style x:Key="TitleContentControlStyle" TargetType="ContentControl" BasedOn="{StaticResource BaseContentControlStyle}">
                                <Setter Property="FontFamily" Value="{DynamicResource PivotTitleFontFamily}" />
                                <Setter Property="FontWeight" Value="{DynamicResource PivotTitleThemeFontWeight}" />
                                <Setter Property="FontSize" Value="{DynamicResource PivotTitleFontSize}" />
                            </Style>
                        </Grid.Resources>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <ContentControl
                            x:Name="TitleContentControl"
                            IsTabStop="False"
                            Margin="{DynamicResource PivotPortraitThemePadding}"
                            Style="{StaticResource TitleContentControlStyle}"
                            Visibility="Collapsed"
                            Content="{TemplateBinding primitives:PivotHelper.Title}"
                            ContentTemplate="{TemplateBinding primitives:PivotHelper.TitleTemplate}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        <Grid
                            Grid.Row="1"
                            Margin="{TemplateBinding Padding}">
                            <Grid.Resources>
                                <ControlTemplate x:Key="NextTemplate" TargetType="ButtonBase">
                                    <Border
                                        x:Name="Root"
                                        Background="{DynamicResource PivotNextButtonBackground}"
                                        BorderThickness="{DynamicResource PivotNavButtonBorderThemeThickness}"
                                        BorderBrush="{DynamicResource PivotNextButtonBorderBrush}">
                                        <local:FontIconFallback
                                            x:Name="Arrow"
                                            FontFamily="{DynamicResource SymbolThemeFontFamily}"
                                            FontSize="12"
                                            Foreground="{DynamicResource PivotNextButtonForeground}"
                                            Data="{StaticResource ChevronRight}"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            UseLayoutRounding="False" />
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="Root" Property="Background" Value="{DynamicResource PivotNextButtonBackgroundPointerOver}" />
                                            <Setter TargetName="Root" Property="BorderBrush" Value="{DynamicResource PivotNextButtonBorderBrushPointerOver}" />
                                            <Setter TargetName="Arrow" Property="Foreground" Value="{DynamicResource PivotNextButtonForegroundPointerOver}" />
                                        </Trigger>
                                        <Trigger Property="IsPressed" Value="True">
                                            <Setter TargetName="Root" Property="Background" Value="{DynamicResource PivotNextButtonBackgroundPressed}" />
                                            <Setter TargetName="Root" Property="BorderBrush" Value="{DynamicResource PivotNextButtonBorderBrushPressed}" />
                                            <Setter TargetName="Arrow" Property="Foreground" Value="{DynamicResource PivotNextButtonForegroundPressed}" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                                <ControlTemplate x:Key="PreviousTemplate" TargetType="ButtonBase">
                                    <Border
                                        x:Name="Root"
                                        Background="{DynamicResource PivotPreviousButtonBackground}"
                                        BorderThickness="{DynamicResource PivotNavButtonBorderThemeThickness}"
                                        BorderBrush="{DynamicResource PivotPreviousButtonBorderBrush}">
                                        <local:FontIconFallback
                                            x:Name="Arrow"
                                            FontFamily="{DynamicResource SymbolThemeFontFamily}"
                                            FontSize="12"
                                            Foreground="{DynamicResource PivotPreviousButtonForeground}"
                                            Data="{StaticResource ChevronLeft}"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            UseLayoutRounding="False" />
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="Root" Property="Background" Value="{DynamicResource PivotPreviousButtonBackgroundPointerOver}" />
                                            <Setter TargetName="Root" Property="BorderBrush" Value="{DynamicResource PivotPreviousButtonBorderBrushPointerOver}" />
                                            <Setter TargetName="Arrow" Property="Foreground" Value="{DynamicResource PivotPreviousButtonForegroundPointerOver}" />
                                        </Trigger>
                                        <Trigger Property="IsPressed" Value="True">
                                            <Setter TargetName="Root" Property="Background" Value="{DynamicResource PivotPreviousButtonBackgroundPressed}" />
                                            <Setter TargetName="Root" Property="BorderBrush" Value="{DynamicResource PivotPreviousButtonBorderBrushPressed}" />
                                            <Setter TargetName="Arrow" Property="Foreground" Value="{DynamicResource PivotPreviousButtonForegroundPressed}" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Grid.Resources>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid
                                x:Name="headerPanel"
                                Grid.Row="0"
                                Background="{TemplateBinding Background}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <ContentPresenter
                                    x:Name="LeftHeaderPresenter"
                                    Content="{TemplateBinding primitives:PivotHelper.LeftHeader}"
                                    ContentTemplate="{TemplateBinding primitives:PivotHelper.LeftHeaderTemplate}"
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                <primitives:PivotHeaderScrollViewer
                                    x:Name="ScrollViewer"
                                    Grid.Column="1"
                                    Style="{DynamicResource {x:Type ScrollViewer}}"
                                    HorizontalScrollBarVisibility="Hidden"
                                    VerticalScrollBarVisibility="Disabled"
                                    VerticalContentAlignment="Stretch"
                                    Template="{DynamicResource ScrollViewerScrollBarlessTemplate}">
                                    <StackPanel
                                        Panel.ZIndex="1"
                                        Background="{DynamicResource PivotHeaderBackground}"
                                        IsItemsHost="true"
                                        Orientation="Horizontal"
                                        KeyboardNavigation.TabIndex="1"
                                        KeyboardNavigation.TabNavigation="Once"
                                        KeyboardNavigation.DirectionalNavigation="Contained" />
                                </primitives:PivotHeaderScrollViewer>
                                <RepeatButton
                                    x:Name="PreviousButton"
                                    Grid.Column="1"
                                    Template="{StaticResource PreviousTemplate}"
                                    Width="20"
                                    Height="36"
                                    FocusVisualStyle="{x:Null}"
                                    Margin="{DynamicResource PivotNavButtonMargin}"
                                    IsTabStop="False"
                                    IsEnabled="False"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Top"
                                    Opacity="0"
                                    Background="Transparent"
                                    Command="{x:Static ScrollBar.LineLeftCommand}"
                                    CommandTarget="{Binding ElementName=ScrollViewer}" />
                                <RepeatButton
                                    x:Name="NextButton"
                                    Grid.Column="1"
                                    Template="{StaticResource NextTemplate}"
                                    Width="20"
                                    Height="36"
                                    FocusVisualStyle="{x:Null}"
                                    Margin="{DynamicResource PivotNavButtonMargin}"
                                    IsTabStop="False"
                                    IsEnabled="False"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Opacity="0"
                                    Background="Transparent"
                                    Command="{x:Static ScrollBar.LineRightCommand}"
                                    CommandTarget="{Binding ElementName=ScrollViewer}" />
                                <ContentPresenter
                                    x:Name="RightHeaderPresenter"
                                    Grid.Column="2"
                                    Content="{TemplateBinding primitives:PivotHelper.RightHeader}"
                                    ContentTemplate="{TemplateBinding primitives:PivotHelper.RightHeaderTemplate}"
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Grid>
                            <Border
                                x:Name="contentPanel"
                                Grid.Row="1"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                KeyboardNavigation.DirectionalNavigation="Contained"
                                KeyboardNavigation.TabIndex="2"
                                KeyboardNavigation.TabNavigation="Local">
                                <ContentPresenter
                                    x:Name="PART_SelectedContentHost"
                                    ContentSource="SelectedContent"
                                    Margin="{DynamicResource PivotItemMargin}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>
                        </Grid>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger SourceName="TitleContentControl" Property="HasContent" Value="True">
                            <Setter TargetName="TitleContentControl" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <!-- PreviousButtonVisible -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="headerPanel" Property="IsMouseOver" Value="True" />
                                <Condition SourceName="contentPanel" Property="IsMouseOver" Value="False" />
                                <Condition SourceName="ScrollViewer" Property="CanScrollLeft" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PreviousButton" Property="Opacity" Value="1" />
                            <Setter TargetName="PreviousButton" Property="IsEnabled" Value="True" />
                        </MultiTrigger>
                        <!-- NextButtonVisible -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="headerPanel" Property="IsMouseOver" Value="True" />
                                <Condition SourceName="contentPanel" Property="IsMouseOver" Value="False" />
                                <Condition SourceName="ScrollViewer" Property="CanScrollRight" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="NextButton" Property="Opacity" Value="1" />
                            <Setter TargetName="NextButton" Property="IsEnabled" Value="True" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>