<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:AppFramework.WindowHost">

    <ControlTemplate x:Key="CardTemplate" TargetType="{x:Type local:Card}">
        <Grid Background="Transparent" Clip="{TemplateBinding ContentClip}">
            <AdornerDecorator>
                <Border CornerRadius="{TemplateBinding UniformCornerRadius}">
                    <Border
                        x:Name="PART_ClipBorder"
                        Padding="{TemplateBinding Padding}"
                        Background="{TemplateBinding Background}"
                        Clip="{TemplateBinding ContentClip}" />
                </Border>
            </AdornerDecorator>
            <ContentPresenter
                x:Name="ContentPresenter"
                Margin="{TemplateBinding Padding}"
                Content="{TemplateBinding ContentControl.Content}"
                ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                ContentTemplateSelector="{TemplateBinding ContentControl.ContentTemplateSelector}" />
        </Grid>
    </ControlTemplate>
    <Style TargetType="{x:Type local:Card}">
        <Setter Property="Template" Value="{StaticResource CardTemplate}" />
        <Setter Property="Focusable" Value="False" />
    </Style>

    <Style x:Key="MaterialDesignDialogHostPopup" TargetType="{x:Type local:PopupEx}">
        <Setter Property="StaysOpen" Value="True" />
        <Setter Property="AllowsTransparency" Value="True" />
        <Setter Property="PopupAnimation" Value="None" />
        <Setter Property="Placement" Value="Center" />
    </Style>

    <Style TargetType="{x:Type local:DialogHost}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="DialogMargin" Value="35" />
        <Setter Property="PopupStyle" Value="{StaticResource MaterialDesignDialogHostPopup}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:DialogHost">
                    <Grid x:Name="DialogHostRoot" Focusable="False">
                        <local:PopupEx
                            x:Name="PART_Popup"
                            PlacementTarget="{Binding ElementName=DialogHostRoot, Mode=OneWay}"
                            Style="{TemplateBinding PopupStyle}">
                            <Grid>
                                <Border Background="Transparent" IsHitTestVisible="{TemplateBinding CloseOnClickAway}">
                                    <Border.InputBindings>
                                        <MouseBinding
                                            Command="{x:Static local:DialogHost.CloseDialogCommand}"
                                            CommandParameter="{TemplateBinding CloseOnClickAwayParameter}"
                                            MouseAction="LeftClick" />
                                    </Border.InputBindings>
                                </Border>
                                <local:Card
                                    x:Name="PART_PopupContentElement"
                                    Margin="{TemplateBinding DialogMargin}"
                                    Background="{Binding DialogBackground, RelativeSource={RelativeSource TemplatedParent}}"
                                    Content="{TemplateBinding DialogContent}"
                                    ContentStringFormat="{TemplateBinding DialogContentStringFormat}"
                                    ContentTemplate="{TemplateBinding DialogContentTemplate}"
                                    ContentTemplateSelector="{TemplateBinding DialogContentTemplateSelector}"
                                    FocusManager.IsFocusScope="False"
                                    Focusable="True"
                                    IsHitTestVisible="True"
                                    IsTabStop="False"
                                    Opacity="0"
                                    RenderTransformOrigin=".5,.5"
                                    UniformCornerRadius="4">
                                    <local:Card.RenderTransform>
                                        <TransformGroup>
                                            <ScaleTransform x:Name="CardScaleTransform" ScaleX="0" ScaleY="0" />
                                        </TransformGroup>
                                    </local:Card.RenderTransform>
                                </local:Card>
                            </Grid>
                        </local:PopupEx>
                        <AdornerDecorator>
                            <ContentPresenter
                                x:Name="ContentPresenter"
                                Content="{TemplateBinding ContentControl.Content}"
                                ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                Opacity="1" />
                        </AdornerDecorator>
                        <Grid
                            x:Name="PART_ContentCoverGrid"
                            Background="{Binding OverlayBackground, RelativeSource={RelativeSource TemplatedParent}}"
                            Focusable="False"
                            IsHitTestVisible="False"
                            Opacity="0">
                            <Grid.Style>
                                <Style TargetType="Grid">
                                    <Style.Triggers>
                                        <Trigger Property="Opacity" Value="0">
                                            <Setter Property="Visibility" Value="Hidden" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Grid.Style>
                        </Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="PopupStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="Closed" To="Open">
                                        <Storyboard>
                                            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="IsOpen">
                                                <DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
                                            </BooleanAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.56">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_PopupContentElement" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleX">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleY">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="Open" To="Closed">
                                        <Storyboard>
                                            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="IsOpen">
                                                <DiscreteBooleanKeyFrame KeyTime="0:0:0.3" Value="False" />
                                            </BooleanAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0.56" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_PopupContentElement" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.18" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleX">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.18" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.5">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleY">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.18" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.5">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Open">
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames
                                            Storyboard.TargetName="PART_Popup"
                                            Storyboard.TargetProperty="IsOpen"
                                            Duration="0">
                                            <DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
                                        </BooleanAnimationUsingKeyFrames>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_ContentCoverGrid"
                                            Storyboard.TargetProperty="Opacity"
                                            To=".56"
                                            Duration="0" />
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_PopupContentElement"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0" />
                                        <DoubleAnimation
                                            Storyboard.TargetName="CardScaleTransform"
                                            Storyboard.TargetProperty="ScaleX"
                                            To="1"
                                            Duration="0" />
                                        <DoubleAnimation
                                            Storyboard.TargetName="CardScaleTransform"
                                            Storyboard.TargetProperty="ScaleY"
                                            To="1"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Closed">
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="IsOpen">
                                            <DiscreteBooleanKeyFrame KeyTime="0" Value="False" />
                                        </BooleanAnimationUsingKeyFrames>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_ContentCoverGrid"
                                            Storyboard.TargetProperty="Opacity"
                                            To="0"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsOpen" Value="True">
                            <Setter TargetName="PART_ContentCoverGrid" Property="IsHitTestVisible" Value="True" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MaterialDesignEmbeddedDialogHost" TargetType="{x:Type local:DialogHost}">
        <Setter Property="DialogMargin" Value="35" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:DialogHost">
                    <Grid x:Name="DialogHostRoot" Focusable="False">
                        <ContentPresenter
                            x:Name="ContentPresenter"
                            Content="{TemplateBinding ContentControl.Content}"
                            ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                            Opacity="1" />

                        <Grid
                            x:Name="PART_ContentCoverGrid"
                            Background="{Binding OverlayBackground, RelativeSource={RelativeSource TemplatedParent}}"
                            Focusable="False"
                            IsHitTestVisible="False"
                            Opacity="0">
                            <Grid.Style>
                                <Style TargetType="Grid">
                                    <Style.Triggers>
                                        <Trigger Property="Opacity" Value="0">
                                            <Setter Property="Visibility" Value="Hidden" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Grid.Style>
                        </Grid>

                        <Grid
                            x:Name="PART_Popup"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <local:Card
                                x:Name="PART_PopupContentElement"
                                Margin="{TemplateBinding DialogMargin}"
                                Background="{Binding DialogBackground, RelativeSource={RelativeSource TemplatedParent}}"
                                Content="{TemplateBinding DialogContent}"
                                ContentStringFormat="{TemplateBinding DialogContentStringFormat}"
                                ContentTemplate="{TemplateBinding DialogContentTemplate}"
                                ContentTemplateSelector="{TemplateBinding DialogContentTemplateSelector}"
                                FocusManager.IsFocusScope="False"
                                Focusable="True"
                                IsTabStop="False"
                                Opacity="0"
                                RenderTransformOrigin=".5,.5"
                                UniformCornerRadius="4">
                                <local:Card.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform x:Name="CardScaleTransform" ScaleX="0" ScaleY="0" />
                                    </TransformGroup>
                                </local:Card.RenderTransform>
                            </local:Card>
                        </Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="PopupStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="Closed" To="Open">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.56">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_PopupContentElement" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleX">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleY">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="Open" To="Closed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Popup" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{x:Static Visibility.Collapsed}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentCoverGrid" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="0.56" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_PopupContentElement" Storyboard.TargetProperty="Opacity">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.18" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleX">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.18" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.5">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CardScaleTransform" Storyboard.TargetProperty="ScaleY">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.18" Value="1" />
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.5">
                                                    <EasingDoubleKeyFrame.EasingFunction>
                                                        <SineEase EasingMode="EaseInOut" />
                                                    </EasingDoubleKeyFrame.EasingFunction>
                                                </EasingDoubleKeyFrame>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Open">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames
                                            Storyboard.TargetName="PART_Popup"
                                            Storyboard.TargetProperty="Visibility"
                                            Duration="0">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_ContentCoverGrid"
                                            Storyboard.TargetProperty="Opacity"
                                            To=".56"
                                            Duration="0" />
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_PopupContentElement"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0" />
                                        <DoubleAnimation
                                            Storyboard.TargetName="CardScaleTransform"
                                            Storyboard.TargetProperty="ScaleX"
                                            To="1"
                                            Duration="0" />
                                        <DoubleAnimation
                                            Storyboard.TargetName="CardScaleTransform"
                                            Storyboard.TargetProperty="ScaleY"
                                            To="1"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Closed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames
                                            Storyboard.TargetName="PART_Popup"
                                            Storyboard.TargetProperty="Visibility"
                                            Duration="0">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_ContentCoverGrid"
                                            Storyboard.TargetProperty="Opacity"
                                            To="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsOpen" Value="True">
                            <Setter TargetName="PART_ContentCoverGrid" Property="IsHitTestVisible" Value="True" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>