<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:Fluent="clr-namespace:Fluent"                    
                    xmlns:converters="clr-namespace:Fluent.Converters"
                    mc:Ignorable="d">

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

    <ControlTemplate x:Key="RibbonWindowControlTemplate"
                     TargetType="{x:Type Fluent:RibbonWindow}">
        <Grid LayoutTransform="{Binding LayoutTransform, RelativeSource={RelativeSource TemplatedParent}}"
              RenderTransform="{Binding RenderTransform, RelativeSource={RelativeSource TemplatedParent}}">
            <AdornerDecorator x:Name="Adorner">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <DockPanel Grid.Row="0"
                               Height="{TemplateBinding TitleBarHeight}"
                               VerticalAlignment="Top"
                               HorizontalAlignment="Stretch"
                               Background="{TemplateBinding TitleBackground}"
                               LastChildFill="True">
                        <Image x:Name="PART_Icon"
                               DockPanel.Dock="Left"
                               Height="16"
                               Width="16"
                               Margin="4,2,0,0"
                               VerticalAlignment="{TemplateBinding VerticalIconAlignment}"
                               Stretch="Uniform"
                               Visibility="{TemplateBinding IsIconVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
                               Source="{Fluent:IconConverter {Binding Icon, RelativeSource={RelativeSource TemplatedParent}}, {Binding RelativeSource={RelativeSource Self}}}"
                               SnapsToDevicePixels="True" />

                        <ContentPresenter x:Name="PART_WindowCommands"
                                          DockPanel.Dock="Right"
                                          Content="{TemplateBinding WindowCommands}"                                          
                                          Grid.Row="0"
                                          Panel.ZIndex="1"
                                          HorizontalAlignment="Right" />

                        <Fluent:RibbonTitleBar x:Name="PART_RibbonTitleBar"
                                               Header="{TemplateBinding Title}"
                                               IsCollapsed="{TemplateBinding IsCollapsed}"
                                               Foreground="{TemplateBinding TitleForeground}" />
                    </DockPanel>

                    <ContentPresenter x:Name="PART_ContentPresenter" 
                                      Grid.Row="1" />
                </Grid>
            </AdornerDecorator>

            <Border x:Name="PART_Border"
                    Background="{x:Null}"
                    BorderBrush="{TemplateBinding GlowBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    UseLayoutRounding="True"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch" />

            <ResizeGrip x:Name="WindowResizeGrip"
                        HorizontalAlignment="Right"
                        IsTabStop="false"
                        Visibility="Collapsed"
                        VerticalAlignment="Bottom" />
        </Grid>

        <ControlTemplate.Triggers>
            <Trigger Property="IsActive"
                     Value="False">
                <Setter Property="Opacity"
                        TargetName="PART_WindowCommands"
                        Value="0.5" />
                <Setter TargetName="PART_Border" Property="BorderBrush" Value="{Binding Path=NonActiveGlowBrush, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsActive" Value="False" />
                    <Condition Property="NonActiveGlowBrush" Value="{x:Null}" />
                </MultiTrigger.Conditions>
                <Setter TargetName="PART_Border" Property="BorderBrush" Value="{Binding Path=NonActiveBorderBrush, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsActive" Value="True" />
                    <Condition Property="GlowBrush" Value="{x:Null}" />
                </MultiTrigger.Conditions>
                <Setter TargetName="PART_Border" Property="BorderBrush" Value="{Binding Path=BorderBrush, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode"
                               Value="CanResizeWithGrip" />
                    <Condition Property="WindowState"
                               Value="Normal" />
                </MultiTrigger.Conditions>
                <Setter Property="Visibility"
                        TargetName="PART_Border"
                        Value="Visible" />
                <Setter Property="Visibility"
                        TargetName="WindowResizeGrip"
                        Value="Visible" />
            </MultiTrigger>
            <Trigger Property="WindowState"
                     Value="Maximized">
                <Setter Property="Margin"
                        TargetName="Adorner"
                        Value="0" />
                <Setter Property="Margin"
                        TargetName="PART_Icon"
                        Value="2,2,0,0" />
                <Setter Property="Visibility"
                        TargetName="PART_Border"
                        Value="Collapsed" />
                <Setter Property="Visibility"
                        TargetName="WindowResizeGrip"
                        Value="Collapsed" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="RibbonWindowStyle"
           TargetType="{x:Type Fluent:RibbonWindow}">
        <Setter Property="BorderBrush"
                Value="{DynamicResource Fluent.Ribbon.Brushes.AccentBaseColorBrush}" />
        <Setter Property="NonActiveBorderBrush"
                Value="#434346" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="GlowBrush"
                Value="{DynamicResource Fluent.Ribbon.Brushes.AccentBaseColorBrush}" />
        <Setter Property="NonActiveGlowBrush"
                Value="#434346" />
        <Setter Property="Background"
                Value="{DynamicResource WhiteBrush}" />
        <Setter Property="Foreground"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="TitleBackground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonWindow.TitleBackground}" />
        <Setter Property="TitleForeground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonWindow.TitleForeground}" />
        <Setter Property="Template"
                Value="{StaticResource RibbonWindowControlTemplate}" />
        <Setter Property="TitleBarHeight"
                Value="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}" />
    </Style>
</ResourceDictionary>
