<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:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:converters="clr-namespace:Fluent.Converters"
                    xmlns:templateSelectors="clr-namespace:Fluent.TemplateSelectors"
                    mc:Ignorable="d">
    <Style TargetType="Separator"
           x:Key="GroupBoxSeparator">
        <Setter Property="Background"
                Value="{DynamicResource Fluent.Ribbon.Brushes.Separator.Background}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Separator">
                    <Border Width="1"
                            Height="55"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Margin="2,5,2,10"
                            Background="{TemplateBinding Background}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="Fluent.Ribbon.Templates.RibbonGroupBox.OneLineHeaderTemplate">
        <TextBlock HorizontalAlignment="Stretch" 
                   VerticalAlignment="Center" 
                   Text="{Binding}" 
                   TextWrapping="NoWrap" 
                   TextTrimming="CharacterEllipsis" 
                   TextAlignment="Center" />
    </DataTemplate>

    <DataTemplate x:Key="Fluent.Ribbon.Templates.RibbonGroupBox.TwoLineHeaderTemplate">
        <Fluent:TwoLineLabel Text="{Binding}"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center"
                             HasGlyph="True" />
    </DataTemplate>

    <Style x:Key="Fluent.Ribbon.Styles.RibbonGroupBox.TwoLineHeaderContentControl"
           TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <ContentPresenter Fluent:RibbonGroupBox.IsCollapsedHeaderContentPresenter="True" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="RibbonGroupBoxStyle"
           TargetType="{x:Type Fluent:RibbonGroupBox}">
        <Setter Property="Template"
                Value="{DynamicResource RibbonGroupBoxControlTemplate}" />
        <Setter Property="Width"
                Value="Auto" />
        <Setter Property="Height"
                Value="Auto" />
        <Setter Property="HorizontalContentAlignment"
                Value="Left" />
        <Setter Property="Foreground"
                Value="{DynamicResource Fluent.Ribbon.Brushes.LabelTextBrush}" />
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="BorderBrush"
                Value="Transparent" />
        <Setter Property="Fluent:KeyTip.Margin"
                Value="0,15,0,0" />
        <Setter Property="HeaderTemplateSelector"
                Value="{Binding Source={x:Static templateSelectors:RibbonGroupBoxHeaderTemplateSelector.Instance}}" />
        <Style.Triggers>
            <Trigger Property="State"
                     Value="QuickAccess">
                <Setter Property="Template"
                        Value="{DynamicResource RibbonGroupBoxDropDownButtonControlTemplate}" />
                <Setter Property="Height"
                        Value="22" />
                <Setter Property="Width"
                        Value="22" />
                <Setter Property="Focusable"
                        Value="False" />
                <Setter Property="SnapsToDevicePixels"
                        Value="False" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="RibbonGroupBoxDropDownButtonControlTemplate"
                     TargetType="{x:Type Fluent:RibbonGroupBox}">
        <Grid Width="22">
            <Border x:Name="PART_ButtonBorder"
                    BorderThickness="1"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Height="Auto"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Stretch">
                <ContentPresenter x:Name="iconImage"
                                  HorizontalAlignment="Center"
                                  Height="16"
                                  VerticalAlignment="Center"
                                  Content="{converters:ObjectToImageConverter {Binding Icon, RelativeSource={RelativeSource TemplatedParent}}, '16,16', {Binding RelativeSource={RelativeSource TemplatedParent}}}"
                                  Width="16"
                                  Margin="1,1,0,0"
                                  SnapsToDevicePixels="True" />
            </Border>

            <Popup x:Name="PART_Popup"
                   Placement="Bottom"
                   IsOpen="{TemplateBinding IsDropDownOpen}"
                   AllowsTransparency="True"
                   Focusable="False"
                   PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                   Margin="0,0,-4,0"
                   VerticalAlignment="Top"
                   Grid.ColumnSpan="2">
                <Grid Width="Auto"
                      Height="Auto"
                      Margin="0,20">
                    <Border x:Name="border"
                            Height="Auto"
                            Background="{DynamicResource Fluent.Ribbon.Brushes.Ribbon.Background}"
                            BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.BorderBrush}"
                            BorderThickness="1"
                            d:LayoutOverrides="Width">
                        <Grid x:Name="PART_ParentPanel"
                              Margin="0,1,0,2"
                              Height="85">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <Fluent:RibbonGroupBoxWrapPanel x:Name="PART_UpPanel"
                                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                            VerticalAlignment="Stretch"
                                                            Width="Auto"
                                                            Height="Auto"
                                                            IsItemsHost="True"
                                                            Margin="2,0,4,0" />

                            <Grid x:Name="PART_DownGrid"
                                  Margin="0,-2,2,2"
                                  Grid.Row="1"
                                  VerticalAlignment="Bottom"
                                  Height="Auto"
                                  MaxWidth="{Binding ActualWidth, ElementName=PART_UpPanel}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <ContentControl Content="{TemplateBinding Header}"
                                                ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"                                                
                                                HorizontalAlignment="Stretch"
                                                VerticalAlignment="Center"
                                                Margin="2,0"
                                                KeyboardNavigation.IsTabStop="False" />

                                <Fluent:Button Margin="0,0,1,1"
                                               x:Name="PART_DialogLauncherButton"
                                               HorizontalAlignment="Stretch"
                                               Width="15"
                                               Height="14"
                                               Grid.Column="1"
                                               Focusable="False"
                                               IsTabStop="False"
                                               Template="{DynamicResource DialogLauncherButtonControlTemplate}"
                                               Background="Transparent"
                                               BorderBrush="Transparent"
                                               VerticalAlignment="Bottom"
                                               Icon="{TemplateBinding LauncherIcon}"
                                               Header="{TemplateBinding LauncherText}"
                                               Command="{Binding LauncherCommand, RelativeSource={RelativeSource TemplatedParent}}"
                                               CommandTarget="{Binding LauncherCommandTarget, RelativeSource={RelativeSource TemplatedParent}}"
                                               CommandParameter="{Binding LauncherCommandParameter, RelativeSource={RelativeSource TemplatedParent}}"
                                               ToolTip="{Binding LauncherToolTip, RelativeSource={RelativeSource TemplatedParent}}"
                                               IsEnabled="{Binding IsLauncherEnabled, RelativeSource={RelativeSource TemplatedParent}}" />
                            </Grid>
                        </Grid>
                    </Border>
                </Grid>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsDropDownOpen"
                     Value="True">
                <Setter Property="BorderBrush"
                        TargetName="PART_ButtonBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.Pressed.BorderBrush}" />
                <Setter Property="Background"
                        TargetName="PART_ButtonBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.Pressed.Background}" />
            </Trigger>
            <Trigger Property="IsEnabled"
                     Value="False">
                <Setter Property="Opacity"
                        TargetName="iconImage"
                        Value="0.5" />
                <Setter Property="Effect"
                        TargetName="iconImage">
                    <Setter.Value>
                        <Fluent:GrayscaleEffect />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver"
                               Value="True" />
                    <Condition Property="IsDropDownOpen"
                               Value="False" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        TargetName="PART_ButtonBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
                <Setter Property="BorderBrush"
                        TargetName="PART_ButtonBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.BorderBrush}" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="RibbonGroupBoxControlTemplate"
                     TargetType="{x:Type Fluent:RibbonGroupBox}">
        <ControlTemplate.Resources>
            <ResourceDictionary>
                <Style TargetType="Separator"
                       BasedOn="{StaticResource GroupBoxSeparator}" />
                <Style x:Key="{x:Type ScrollBar}"
                       TargetType="{x:Type ScrollBar}"
                       BasedOn="{StaticResource ScrollBarWhite}" />
                <Style x:Key="{x:Type ScrollViewer}"
                       TargetType="{x:Type ScrollViewer}"
                       BasedOn="{StaticResource ScrollViewerStyle}" />
            </ResourceDictionary>
        </ControlTemplate.Resources>

        <Grid x:Name="grid2"
              HorizontalAlignment="Stretch">
            <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}" />

            <Grid>
                <ContentControl x:Name="nonCollapsedStateBorder"
                                Focusable="False">
                    <Grid x:Name="PART_ParentPanel"
                          Margin="0,1,0,2"
                          Height="Auto">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Fluent:RibbonGroupBoxWrapPanel x:Name="PART_UpPanel"
                                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                        VerticalAlignment="Stretch"
                                                        Width="Auto"
                                                        Height="Auto"
                                                        IsItemsHost="True"
                                                        Margin="4,0,4,0" />

                        <Grid x:Name="PART_DownGrid"
                              MaxWidth="{Binding ActualWidth, ElementName=PART_ParentPanel}"
                              Margin="0,-2,2,2"
                              Grid.Row="1"
                              VerticalAlignment="Bottom"
                              Height="Auto">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <ContentControl x:Name="PART_HeaderContentControl" 
                                            Content="{TemplateBinding Header}"
                                            ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                            ContentTemplate="{TemplateBinding HeaderTemplate}"
                                            ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                            Foreground="{DynamicResource Fluent.Ribbon.Brushes.RibbonGroupBox.Header.Foreground}"
                                            HorizontalAlignment="Stretch"
                                            VerticalAlignment="Center"
                                            Margin="2,0"
                                            KeyboardNavigation.IsTabStop="False" />

                            <Fluent:Button Margin="0,0,1,1"
                                           x:Name="PART_DialogLauncherButton"
                                           HorizontalAlignment="Stretch"
                                           Width="15"
                                           Height="14"
                                           Grid.Column="1"
                                           Focusable="False"
                                           IsTabStop="False"
                                           Template="{DynamicResource DialogLauncherButtonControlTemplate}"
                                           Background="Transparent"
                                           BorderBrush="Transparent"
                                           VerticalAlignment="Bottom"
                                           Icon="{TemplateBinding LauncherIcon}"
                                           Header="{TemplateBinding LauncherText}"
                                           Command="{Binding LauncherCommand, RelativeSource={RelativeSource TemplatedParent}}"
                                           CommandTarget="{Binding LauncherCommandTarget, RelativeSource={RelativeSource TemplatedParent}}"
                                           CommandParameter="{Binding LauncherCommandParameter, RelativeSource={RelativeSource TemplatedParent}}"
                                           ToolTip="{Binding LauncherToolTip, RelativeSource={RelativeSource TemplatedParent}}"
                                           IsEnabled="{Binding IsLauncherEnabled, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Grid>
                    </Grid>
                </ContentControl>

                <Grid x:Name="collapsedStateGrid"
                      Margin="0"
                      Width="Auto"
                      Height="Auto"
                      Visibility="Collapsed"
                      Grid.RowSpan="2">

                    <ContentControl x:Name="PART_CollapsedHeaderContentControl"
                                    Content="{TemplateBinding Header}"
                                    ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                    ContentTemplate="{TemplateBinding HeaderTemplate}"
                                    ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                    Foreground="{DynamicResource Fluent.Ribbon.Brushes.RibbonGroupBox.Header.Foreground}"
                                    Style="{DynamicResource Fluent.Ribbon.Styles.RibbonGroupBox.TwoLineHeaderContentControl}"
                                    Fluent:RibbonGroupBox.IsCollapsedHeaderContentPresenter="True"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Top"
                                    Margin="5,38,5,0"
                                    KeyboardNavigation.IsTabStop="False" />

                    <Border HorizontalAlignment="Center"
                            VerticalAlignment="Top"
                            Width="31"
                            Height="31"
                            BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.RibbonGroupBox.Collapsed.BorderBrush}"
                            BorderThickness="1"
                            Margin="5,3,8,0">
                        <ContentPresenter x:Name="iconImage"
                                          Height="16"
                                          Content="{converters:ObjectToImageConverter {Binding Icon, RelativeSource={RelativeSource TemplatedParent}}, '16,16', {Binding RelativeSource={RelativeSource TemplatedParent}}}"
                                          Width="16"
                                          SnapsToDevicePixels="True" />
                    </Border>

                    <Popup x:Name="PART_Popup"
                           Width="Auto"
                           Height="{Binding ActualHeight, RelativeSource={RelativeSource TemplatedParent}}"
                           AllowsTransparency="True"
                           PlacementTarget="{Binding ElementName=collapsedStateGrid, Mode=OneWay}"
                           IsOpen="{TemplateBinding IsDropDownOpen}">
                        <Border x:Name="border"
                                Height="Auto"
                                Background="{DynamicResource Fluent.Ribbon.Brushes.Ribbon.Background}"
                                BorderBrush="{DynamicResource Fluent.Ribbon.Brushes.RibbonTabItem.BorderBrush}"
                                BorderThickness="1"
                                d:LayoutOverrides="Width">
                            <ContentControl x:Name="popupContent" />
                        </Border>
                    </Popup>
                </Grid>

                <Rectangle x:Name="separator"
                           Fill="{DynamicResource Fluent.Ribbon.Brushes.GroupSeparator.Background}"
                           HorizontalAlignment="Right"
                           VerticalAlignment="Stretch"
                           Width="1"
                           Height="Auto"
                           Grid.RowSpan="2"
                           Margin="0,4"
                           Visibility="{Binding IsSeparatorVisible, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}}" />
            </Grid>

            <Image x:Name="PART_SnappedImage"
                   Stretch="Uniform"
                   Visibility="Collapsed" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="State"
                     Value="Collapsed">
                <Setter Property="Visibility"
                        TargetName="collapsedStateGrid"
                        Value="Visible" />
                <Setter Property="UIElement.Visibility"
                        TargetName="nonCollapsedStateBorder"
                        Value="Collapsed" />
                <Setter Property="HorizontalAlignment"
                        TargetName="grid2"
                        Value="Left" />
                <Setter Property="Content"
                        TargetName="nonCollapsedStateBorder"
                        Value="{x:Null}" />
                <Setter Property="Content"
                        TargetName="popupContent"
                        Value="{Binding ElementName=PART_ParentPanel}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="State"
                               Value="Collapsed" />
                    <Condition Property="IsMouseOver"
                               Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonGroupBox.Collapsed.MouseOver.Background}" />
            </MultiTrigger>
            <Trigger Property="IsDropDownOpen"
                     Value="True">
                <Setter Property="Background"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.RibbonGroupBox.DropDownOpen.Background}" />
            </Trigger>
            <Trigger Property="IsLauncherVisible"
                     Value="False">
                <Setter Property="Visibility"
                        TargetName="PART_DialogLauncherButton"
                        Value="Collapsed" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="DialogLauncherButtonControlTemplate"
                     TargetType="{x:Type Fluent:Button}">
        <Border x:Name="outerBorder"
                BorderThickness="1"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}">
            <Image x:Name="image"
                   Width="8"
                   Height="8"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Stretch="Uniform"
                   Source="{DynamicResource Fluent.Ribbon.Images.DialogLauncher}"
                   SnapsToDevicePixels="True">
            </Image>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="FlowDirection"
                     Value="RightToLeft">
                <Setter Property="RenderTransform"
                        TargetName="image">
                    <Setter.Value>
                        <ScaleTransform ScaleX="-1" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsPressed"
                     Value="True">
                <Setter Property="Background"
                        TargetName="outerBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.Pressed.Background}" />
                <Setter Property="BorderBrush"
                        TargetName="outerBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.Pressed.BorderBrush}" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver"
                               Value="True" />
                    <Condition Property="IsPressed"
                               Value="False" />
                </MultiTrigger.Conditions>
                <Setter Property="Background"
                        TargetName="outerBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
                <Setter Property="BorderBrush"
                        TargetName="outerBorder"
                        Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.BorderBrush}" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>