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

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/Thumb.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <DataTemplate x:Key="HeaderTemplate" x:Shared="False">
        <DockPanel x:Name="dpHeader"
                   VerticalAlignment="Center"
                   LastChildFill="True">
            <Button x:Name="PART_BackButton"
                    Width="34"
                    Height="34"
                    Margin="2 4 2 2"
                    VerticalAlignment="Bottom"
                    DockPanel.Dock="Left"
                    Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Flyout}}, Path=Foreground}"
                    IsCancel="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:Flyout}}, Path=CloseButtonIsCancel}"
                    Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}"
                    Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:Flyout}}, Path=CloseButtonVisibility}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <actions:CloseFlyoutAction Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Flyout}}, Path=CloseCommand, Mode=OneWay}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:Flyout}}, Path=CloseCommandParameter, Mode=OneWay}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ContentControl Width="20"
                                Height="20"
                                Content="M19,34V42H43.75L33.75,52H44.25L58.25,38L44.25,24H33.75L43.75,34H19Z"
                                FlowDirection="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:Flyout}}, Path=FlowDirection}"
                                Style="{DynamicResource PathIconContentControlStyle}" />
            </Button>
            <ContentControl x:Name="PART_BackHeaderText"
                            Margin="15 0 0 0"
                            VerticalAlignment="Center"
                            Content="{Binding}"
                            Focusable="False"
                            IsTabStop="False"
                            Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:Flyout}}, Path=TitleVisibility}" />
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Position, RelativeSource={RelativeSource AncestorType={x:Type Controls:Flyout}}}" Value="Left">
                <Setter TargetName="PART_BackButton" Property="DockPanel.Dock" Value="Right" />
                <Setter TargetName="PART_BackButton" Property="LayoutTransform">
                    <Setter.Value>
                        <ScaleTransform ScaleX="-1" />
                    </Setter.Value>
                </Setter>
                <Setter TargetName="PART_BackHeaderText" Property="HorizontalAlignment" Value="Right" />
                <Setter TargetName="PART_BackHeaderText" Property="Margin" Value="0 0 15 0" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Position, RelativeSource={RelativeSource AncestorType={x:Type Controls:Flyout}}}" Value="Top">
                <Setter TargetName="PART_BackButton" Property="LayoutTransform">
                    <Setter.Value>
                        <RotateTransform Angle="-90" />
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Position, RelativeSource={RelativeSource AncestorType={x:Type Controls:Flyout}}}" Value="Bottom">
                <Setter TargetName="PART_BackButton" Property="LayoutTransform">
                    <Setter.Value>
                        <RotateTransform Angle="90" />
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <ControlTemplate x:Key="FlyoutTemplate" TargetType="{x:Type Controls:Flyout}">
        <Border x:Name="PART_Root"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}">
            <Border.RenderTransform>
                <TranslateTransform />
            </Border.RenderTransform>
            <AdornerDecorator>
                <DockPanel FocusVisualStyle="{x:Null}" Focusable="False">
                    <Controls:MetroThumbContentControl x:Name="PART_Header"
                                                       Padding="{TemplateBinding Controls:ControlsHelper.HeaderMargin}"
                                                       Content="{TemplateBinding Header}"
                                                       ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                                       ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                       ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                       ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                                       DockPanel.Dock="Top"
                                                       FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
                                                       FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
                                                       FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
                                                       RecognizesAccessKey="True"
                                                       SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    <ContentPresenter x:Name="PART_Content"
                                      ContentSource="Content"
                                      DockPanel.Dock="Bottom" />
                </DockPanel>
            </AdornerDecorator>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="Default" />
                    <VisualState x:Name="Hide">
                        <Storyboard x:Name="HideStoryboard">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                <SplineDoubleKeyFrame x:Name="hideFrame"
                                                      KeySpline="0.25,1,0.05,1"
                                                      KeyTime="00:00:00.750"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
                                <SplineDoubleKeyFrame x:Name="hideFrameY"
                                                      KeySpline="0.25,1,0.05,1"
                                                      KeyTime="00:00:00.750"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame x:Name="fadeOutFrame"
                                                      KeySpline="0.25,1,0.05,1"
                                                      KeyTime="00:00:00.750"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Show">
                        <Storyboard x:Name="ShowStoryboard">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                <SplineDoubleKeyFrame x:Name="showFrame"
                                                      KeySpline="0.25,1,0.05,1"
                                                      KeyTime="00:00:00.750"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
                                <SplineDoubleKeyFrame x:Name="showFrameY"
                                                      KeySpline="0.25,1,0.05,1"
                                                      KeyTime="00:00:00.750"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeySpline="0.25,1,0.05,1"
                                                      KeyTime="00:00:00.750"
                                                      Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="HideDirect">
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="ShowDirect">
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                           Storyboard.TargetName="PART_Root"
                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                <SplineDoubleKeyFrame KeyTime="0" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Position" Value="Top">
                <Setter TargetName="PART_Content" Property="DockPanel.Dock" Value="Right" />
                <Setter TargetName="PART_Header" Property="DockPanel.Dock" Value="Left" />
            </Trigger>
            <Trigger Property="Position" Value="Bottom">
                <Setter TargetName="PART_Content" Property="DockPanel.Dock" Value="Right" />
                <Setter TargetName="PART_Header" Property="DockPanel.Dock" Value="Left" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="CloseButtonVisibility" Value="Collapsed" />
                    <Condition Property="TitleVisibility" Value="Collapsed" />
                </MultiTrigger.Conditions>
                <Setter TargetName="PART_Header" Property="Visibility" Value="Collapsed" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="CloseButtonVisibility" Value="Hidden" />
                    <Condition Property="TitleVisibility" Value="Hidden" />
                </MultiTrigger.Conditions>
                <Setter TargetName="PART_Header" Property="Visibility" Value="Collapsed" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="{x:Type Controls:Flyout}">
        <Setter Property="Background" Value="{DynamicResource FlyoutBackgroundBrush}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="{DynamicResource FlyoutHeaderFontSize}" />
        <Setter Property="Controls:ControlsHelper.HeaderMargin" Value="10" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Foreground" Value="{DynamicResource FlyoutForegroundBrush}" />
        <Setter Property="HeaderTemplate" Value="{StaticResource HeaderTemplate}" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="KeyboardNavigation.ControlTabNavigation" Value="Cycle" />
        <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle" />
        <Setter Property="Template" Value="{StaticResource FlyoutTemplate}" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="Visibility" Value="Hidden" />
        <Style.Triggers>
            <Trigger Property="Position" Value="Right">
                <Setter Property="Controls:ControlsHelper.HeaderMargin" Value="10 25 10 10" />
            </Trigger>
            <Trigger Property="Position" Value="Left">
                <Setter Property="Controls:ControlsHelper.HeaderMargin" Value="10 25 10 10" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

