<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:bpa="clr-namespace:BPA.UIControl">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/BPA.UIControl;component/Themes/Button.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="TitleBarButtonStyle"
           BasedOn="{StaticResource TextButton}"
           TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="{Binding TitleForeground, RelativeSource={RelativeSource AncestorType=bpa:BPAWindow}}" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Padding" Value="12 5" />
        <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
    </Style>

    <Style x:Key="TitleBarCloseButtonStyle"
           BasedOn="{StaticResource TitleBarButtonStyle}"
           TargetType="{x:Type Button}">
        <Setter Property="bpa:ControlHelper.FocusedBrush" Value="Red" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{DynamicResource Error}" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="{x:Type bpa:BPAWindow}">
        <Setter Property="Background" Value="{DynamicResource DefaultBackground}" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Foreground" Value="{DynamicResource DefaultForeground}" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="ResizeMode" Value="CanResizeWithGrip" />
        <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
        <Setter Property="UseLayoutRounding" Value="True" />
        <Setter Property="WindowStyle" Value="SingleBorderWindow" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="TitleShadow" Value="False" />
        <Setter Property="TitleHeight" Value="24" />
        <Setter Property="TitleBarContent" Value="{x:Null}" />
        <Setter Property="TitleForeground" Value="{DynamicResource WhiteForeground}" />
        <Setter Property="TitleBackground" Value="{DynamicResource Primary}" />
        <Setter Property="InactiveBorderBrush" Value="{DynamicResource Light}" />
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CaptionHeight="{Binding TitleHeight, FallbackValue=30, RelativeSource={RelativeSource AncestorType=bpa:BPAWindow}}"
                              CornerRadius="0"
                              GlassFrameThickness="1"
                              NonClientFrameEdges="None"
                              UseAeroCaptionButtons="False" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type bpa:BPAWindow}">
                    <Border x:Name="WindowBorder"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition x:Name="TitleRow" Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <Grid x:Name="PART_WindowTitleGrid"
                                  Grid.Row="0"
                                  Height="{TemplateBinding TitleHeight}"
                                  Panel.ZIndex="100"
                                  Background="{TemplateBinding TitleBackground}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Border Grid.ColumnSpan="2"
                                        Background="{TemplateBinding BorderBrush}"
                                        Effect="{DynamicResource BottomEffect}"
                                        Visibility="{TemplateBinding TitleShadow, Converter={StaticResource BooleanToVisibleConverter}}" />
                                <!--  默认 window 的左边内容：图标 + 标题  -->
                                <Grid Grid.Column="0" TextBlock.Foreground="{TemplateBinding TitleForeground}">
                                    <StackPanel x:Name="defalutTitle"
                                                Orientation="Horizontal"
                                                Visibility="Collapsed">
                                        <Image x:Name="icon"
                                               Width="22"
                                               Height="22"
                                               Margin="10 0 0 0"
                                               VerticalAlignment="Center"
                                               Source="{TemplateBinding Icon}" />
                                        <TextBlock Margin="10 0 0 0"
                                                   FontSize="13"
                                                   Text="{TemplateBinding Title}" />
                                    </StackPanel>
                                    <ContentControl x:Name="preContent"
                                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                    Content="{TemplateBinding TitleBarContent}" />
                                </Grid>

                                <!--  标题栏右边按钮  -->
                                <StackPanel x:Name="WindowCommandButtonsStackPanel"
                                            Grid.Column="1"
                                            HorizontalAlignment="Right"
                                            Background="Transparent"
                                            Orientation="Horizontal"
                                            WindowChrome.IsHitTestVisibleInChrome="True">
                                    <Button x:Name="Minimize"
                                            Style="{StaticResource TitleBarButtonStyle}"
                                            Command="{Binding Source={x:Static SystemCommands.MinimizeWindowCommand}}">
                                        <bpa:Icon Type="MinimizeLine" />
                                    </Button>
                                    <Button x:Name="Restore"
                                            Style="{StaticResource TitleBarButtonStyle}"
                                            Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}"
                                            Visibility="Collapsed">
                                        <bpa:Icon Type="RestoreLine" />
                                    </Button>
                                    <Button x:Name="Maximize"
                                            Style="{StaticResource TitleBarButtonStyle}"
                                            Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}">
                                        <bpa:Icon Type="MaximizeLine" />
                                    </Button>
                                    <Button x:Name="Close"
                                            Style="{StaticResource TitleBarCloseButtonStyle}"
                                            Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}">
                                        <bpa:Icon Type="CloseLine" />
                                    </Button>
                                </StackPanel>
                            </Grid>

                            <AdornerDecorator Grid.Row="1" KeyboardNavigation.IsTabStop="False">
                                <bpa:NotificationContainer>
                                    <bpa:MessageContainer>
                                        <bpa:MessageBoxContainer>
                                            <bpa:DialogContainer x:Name="root">
                                                <ContentPresenter x:Name="MainContentPresenter" KeyboardNavigation.TabNavigation="Cycle" />
                                            </bpa:DialogContainer>
                                        </bpa:MessageBoxContainer>
                                    </bpa:MessageContainer>
                                </bpa:NotificationContainer>
                            </AdornerDecorator>

                            <ResizeGrip x:Name="ResizeGrip"
                                        Grid.Row="1"
                                        HorizontalAlignment="Right"
                                        VerticalAlignment="Bottom"
                                        IsTabStop="False"
                                        Visibility="Hidden"
                                        WindowChrome.ResizeGripDirection="BottomRight" />
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsActive" Value="False">
                            <Setter Property="BorderBrush" Value="{Binding InactiveBorderBrush, RelativeSource={RelativeSource Self}}" />
                        </Trigger>
                        <Trigger Property="WindowState" Value="Maximized">
                            <Setter TargetName="LayoutRoot" Property="Margin" Value="7" />
                            <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Restore" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <Trigger Property="WindowState" Value="Normal">
                            <Setter TargetName="Maximize" Property="Visibility" Value="Visible" />
                            <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="ResizeMode" Value="NoResize">
                            <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Minimize" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="ResizeMode" Value="CanMinimize">
                            <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Minimize" Property="Visibility" Value="Visible" />
                            <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="WindowStyle" Value="ToolWindow">
                            <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Minimize" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="WindowStyle" Value="None">
                            <Setter TargetName="TitleRow" Property="Height" Value="0" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="ResizeMode" Value="CanResizeWithGrip" />
                                <Condition Property="WindowState" Value="Normal" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ResizeGrip" Property="Visibility" Value="Visible" />
                        </MultiTrigger>
                        <Trigger Property="TitleBarContent" Value="{x:Null}">
                            <Setter TargetName="defalutTitle" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <Trigger Property="Icon" Value="{x:Null}">
                            <Setter TargetName="icon" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>