<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:FluentHelpers="clr-namespace:Fluent.Helpers"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:converters="clr-namespace:Fluent.Converters"
                    xmlns:styleSelectors="clr-namespace:Fluent.StyleSelectors"
                    mc:Ignorable="d">
    <ControlTemplate x:Key="ApplicationMenuButtonControlTemplate"
                     TargetType="{x:Type Fluent:ApplicationMenu}">
        <Grid Margin="0">
            <ContentControl x:Name="buttonContent"
                            Width="{Binding ActualWidth, ElementName=PART_ButtonBorder}"
                            Height="{Binding ActualHeight, ElementName=PART_ButtonBorder}"
                            Margin="0">
                <Border x:Name="PART_ButtonBorder"
                        MinWidth="60"
                        Height="23"
                        Background="{TemplateBinding Background}">
                    <ContentControl x:Name="contentPresenter"
                                    Content="{TemplateBinding Header}"
                                    Margin="10,0,10,0"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center" />
                </Border>
            </ContentControl>
            <Popup x:Name="PART_Popup"
                   Placement="Custom"
                   CustomPopupPlacementCallback="{x:Static FluentHelpers:PopupHelper.SimplePlacementCallback}"
                   PlacementTarget="{Binding ElementName=buttonContent}"
                   VerticalOffset="{Binding ElementName=buttonContent, Path=ActualHeight}"
                   IsOpen="{TemplateBinding IsDropDownOpen}"
                   MaxHeight="{TemplateBinding MaxDropDownHeight}"
                   AllowsTransparency="True"
                   Focusable="False"
                   Margin="0,0,-4,0"
                   VerticalAlignment="Top"
                   Grid.ColumnSpan="2">
                <Grid Name="DropDown"
                      SnapsToDevicePixels="True"
                      Margin="0"
                      VerticalAlignment="Top">
                    <Border x:Name="PART_DropDownBorder"
                            MinWidth="{TemplateBinding ActualWidth}"
                            SnapsToDevicePixels="True"
                            Background="{DynamicResource Fluent.Ribbon.Brushes.DropDown.BackgroundBrush}"
                            BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.DropDown.BorderBrush}"
                            BorderThickness="1"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Top">
                        <Grid Width="Auto"
                              Height="Auto"
                              HorizontalAlignment="Stretch"
                              VerticalAlignment="Top">
                            <Grid HorizontalAlignment="Stretch"
                                  VerticalAlignment="Top">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="Auto"
                                                   MinHeight="17" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"
                                                      MinWidth="100" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <ScrollViewer Style="{DynamicResource MenuScrollViewer}"
                                              Grid.Row="0"
                                              Grid.Column="0"
                                              IsTabStop="False"
                                              ScrollViewer.VerticalScrollBarVisibility="Auto"
                                              ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                                    <StackPanel Orientation="Vertical"
                                                ScrollViewer.VerticalScrollBarVisibility="Auto"
                                                IsItemsHost="True"
                                                KeyboardNavigation.DirectionalNavigation="Cycle" />
                                </ScrollViewer>

                                <Border Grid.Row="0"
                                        Grid.Column="1"
                                        BorderThickness="1,0,0,0"
                                        BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.Separator.BorderBrush}"
                                        Width="{TemplateBinding RightPaneWidth}">
                                    <ScrollViewer x:Name="PART_ScrollViewer"
                                                  Style="{DynamicResource MenuScrollViewer}"
                                                  IsTabStop="False"
                                                  Margin="1"                                                  
                                                  SnapsToDevicePixels="True"
                                                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                                                  ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                                        <ContentPresenter ContentSource="RightPaneContent"
                                                          x:Name="PART_RightContentPresenter" />
                                    </ScrollViewer>
                                </Border>
                                <Border Grid.Row="1"
                                        Grid.ColumnSpan="2"
                                        BorderThickness="0,1,0,0"
                                        BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.Separator.BorderBrush}"
                                        Background="{DynamicResource Fluent.Ribbon.Brushes.Ribbon.Background}"
                                        Margin="0">
                                    <ContentPresenter ContentSource="FooterPaneContent" />
                                </Border>
                            </Grid>
                        </Grid>
                    </Border>
                </Grid>
            </Popup>
        </Grid>

        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled"
                     Value="False">
                <Setter Property="Opacity"
                        TargetName="PART_ButtonBorder"
                        Value="0.5" />
                <Setter Property="Effect"
                        TargetName="contentPresenter">
                    <Setter.Value>
                        <Fluent:GrayscaleEffect />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="{x:Type Fluent:ApplicationMenu}">
        <Setter Property="Template" Value="{DynamicResource ApplicationMenuButtonControlTemplate}" />
        <Setter Property="Background" Value="{DynamicResource Fluent.Ribbon.Brushes.AccentBaseColorBrush}" />
        <Setter Property="Foreground" Value="{DynamicResource Fluent.Ribbon.Brushes.IdealForegroundColorBrush}" />
        <Setter Property="Header" Value="{converters:ObjectToImageConverter {DynamicResource Fluent.Ribbon.Images.ApplicationMenu}, '40,16', {Binding RelativeSource={RelativeSource Self}}}" />
        <Setter Property="ItemContainerStyleSelector" Value="{x:Static styleSelectors:ApplicationMenuItemContainerStyleSelector.Instance}" />
    </Style>
</ResourceDictionary>