<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">

    <ControlTemplate x:Key="WindowCommandsButtonTemplate" TargetType="{x:Type Button}">
        <Grid Background="{TemplateBinding Background}">
            <Controls:ContentControlEx x:Name="PART_ContentControlEx"
                                       Padding="{TemplateBinding Padding}"
                                       HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Content="{TemplateBinding Content}"
                                       ContentCharacterCasing="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                       ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                       ContentTemplate="{TemplateBinding ContentTemplate}"
                                       ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                       Opacity="0.5"
                                       RecognizesAccessKey="True"
                                       SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="PART_ContentControlEx" Property="Opacity" Value="1" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="WindowCommandsToggleButtonTemplate" TargetType="{x:Type ToggleButton}">
        <Grid Background="{TemplateBinding Background}">
            <Controls:ContentControlEx x:Name="PART_ContentControlEx"
                                       Padding="{TemplateBinding Padding}"
                                       HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Content="{TemplateBinding Content}"
                                       ContentCharacterCasing="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                       ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                       ContentTemplate="{TemplateBinding ContentTemplate}"
                                       ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                       Opacity="0.5"
                                       RecognizesAccessKey="True"
                                       SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter TargetName="PART_ContentControlEx" Property="Opacity" Value="0.8" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="PART_ContentControlEx" Property="Opacity" Value="1" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="WindowCommandsControlStyle" TargetType="{x:Type Control}">
        <Setter Property="Background" Value="{DynamicResource TransparentWhiteBrush}" />
        <Setter Property="Foreground" Value="{Binding Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}}" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Padding" Value="8 0 8 0" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource GrayBrush4}" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="LightWindowCommandsTemplate" TargetType="Controls:WindowCommands">
        <ItemsPresenter />
        <ControlTemplate.Resources>
            <ResourceDictionary>
                <Style BasedOn="{StaticResource WindowCommandsControlStyle}" TargetType="{x:Type Button}">
                    <Setter Property="Template" Value="{StaticResource WindowCommandsButtonTemplate}" />
                </Style>
                <Style BasedOn="{StaticResource WindowCommandsControlStyle}" TargetType="{x:Type ToggleButton}">
                    <Setter Property="Template" Value="{StaticResource WindowCommandsToggleButtonTemplate}" />
                </Style>
            </ResourceDictionary>
        </ControlTemplate.Resources>
    </ControlTemplate>

    <ControlTemplate x:Key="DarkWindowCommandsTemplate" TargetType="Controls:WindowCommands">
        <ItemsPresenter />
        <ControlTemplate.Resources>
            <ResourceDictionary>
                <Style BasedOn="{StaticResource WindowCommandsControlStyle}" TargetType="{x:Type Button}">
                    <Setter Property="Template" Value="{StaticResource WindowCommandsButtonTemplate}" />
                    <Style.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Foreground" Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
                <Style BasedOn="{StaticResource WindowCommandsControlStyle}" TargetType="{x:Type ToggleButton}">
                    <Setter Property="Template" Value="{StaticResource WindowCommandsToggleButtonTemplate}" />
                    <Style.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Foreground" Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ResourceDictionary>
        </ControlTemplate.Resources>
    </ControlTemplate>

    <Style TargetType="{x:Type Controls:WindowCommands}">
        <Setter Property="Background" Value="{DynamicResource TransparentWhiteBrush}" />
        <Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Normal" />
        <Setter Property="DarkTemplate" Value="{StaticResource DarkWindowCommandsTemplate}" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Foreground" Value="{DynamicResource BlackBrush}" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type Controls:WindowCommandsItem}">
                    <Setter Property="IsTabStop" Value="False" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Controls:WindowCommandsItem}">
                                <StackPanel HorizontalAlignment="{Binding HorizontalContentAlignment, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}}"
                                            VerticalAlignment="{Binding VerticalContentAlignment, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}}"
                                            Orientation="Horizontal">
                                    <ContentPresenter x:Name="PART_ContentPresenter"
                                                      Margin="1 0"
                                                      Content="{TemplateBinding Content}"
                                                      ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                                      ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                    <Rectangle x:Name="PART_Separator"
                                               Width="1"
                                               Height="{Binding SeparatorHeight, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}}"
                                               Fill="{Binding Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Controls:WindowCommands}}}"
                                               IsHitTestVisible="False"
                                               Opacity="0.25"
                                               SnapsToDevicePixels="True"
                                               UseLayoutRounding="True" />
                                </StackPanel>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSeparatorVisible" Value="False">
                                        <Setter TargetName="PART_Separator" Property="Visibility" Value="Collapsed" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="LightTemplate" Value="{StaticResource LightWindowCommandsTemplate}" />
        <Setter Property="Template" Value="{StaticResource LightWindowCommandsTemplate}" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding ParentWindow.ShowTitleBar, RelativeSource={RelativeSource Self}}" Value="True">
                <Setter Property="Foreground" Value="{DynamicResource IdealForegroundColorBrush}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>
