<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:root="clr-namespace:ModernWpf"
    xmlns:local="clr-namespace:ModernWpf.Controls"
    xmlns:primitives="clr-namespace:ModernWpf.Controls.Primitives"
    xmlns:animation="clr-namespace:ModernWpf.Media.Animation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:Double x:Key="ProgressBarMinHeight">3</sys:Double>
    <sys:Double x:Key="ProgressBarTrackHeight">1</sys:Double>
    <CornerRadius x:Key="ProgressBarCornerRadius">1.5</CornerRadius>
    <CornerRadius x:Key="ProgressBarTrackCornerRadius">0.5</CornerRadius>

    <primitives:CornerRadiusFilterConverter x:Key="TopLeftCornerRadiusDoubleValueConverter" Filter="TopLeftValue" />
    <primitives:CornerRadiusFilterConverter x:Key="BottomRightCornerRadiusDoubleValueConverter" Filter="BottomRightValue" />

    <Style TargetType="local:ProgressBar">
        <Setter Property="Foreground" Value="{DynamicResource ProgressBarForeground}" />
        <Setter Property="Background" Value="{DynamicResource ProgressBarBackground}" />
        <Setter Property="BorderThickness" Value="{DynamicResource ProgressBarBorderThemeThickness}" />
        <Setter Property="BorderBrush" Value="{DynamicResource ProgressBarBorderBrush}" />
        <Setter Property="MinHeight" Value="{StaticResource ProgressBarMinHeight}" />
        <Setter Property="Maximum" Value="100" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="CornerRadius" Value="{StaticResource ProgressBarCornerRadius}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:ProgressBar">

                    <Grid x:Name="LayoutRoot" SnapsToDevicePixels="True">
                        <Grid.Resources>
                            <Storyboard x:Key="UpdatingToDeterminateStoryboard">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DeterminateProgressBarIndicator" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <DiscreteDoubleKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.IndicatorLengthDelta}" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.367" Value="0" KeySpline="0.1,0.9 0.2,1" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="UpdatingErrorToErrorStoryboard">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DeterminateProgressBarIndicator" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <DiscreteDoubleKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.IndicatorLengthDelta}" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.367" Value="0" KeySpline="0.1,0.9 0.2,1" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="PausedToDeterminateStoryboard">
                                <Storyboard>
                                    <ColorAnimation
                                        Storyboard.TargetName="DeterminateProgressBarIndicator"
                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                        To="{Binding Foreground.Color, RelativeSource={RelativeSource TemplatedParent}}"
                                        Duration="0:0:0.167"/>
                                </Storyboard>
                            </Storyboard>
                            <Storyboard x:Key="ErrorToDeterminateStoryboard">
                                <ColorAnimation
                                    Storyboard.TargetName="DeterminateProgressBarIndicator"
                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                    To="{Binding Foreground.Color, RelativeSource={RelativeSource TemplatedParent}}"
                                    Duration="0:0:0.167"/>
                            </Storyboard>
                            <Storyboard x:Key="IndeterminatePausedToIndeterminateStoryboard">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="IndeterminateProgressBarIndicator">
                                    <DiscreteDoubleKeyFrame KeyTime="0" Value="0" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="IndeterminateProgressBarIndicator2"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.333" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationEndPosition}" KeySpline="1.0, 0.0, 1.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="ProgressBarTrack"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0" KeySpline="1.0, 0.0, 1.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationEndPosition}" KeySpline="1.0, 0.0, 1.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="IndeterminateErrorToIndeterminateStoryboard">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="IndeterminateProgressBarIndicator">
                                    <DiscreteDoubleKeyFrame KeyTime="0" Value="0" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="IndeterminateProgressBarIndicator2"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.333" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationEndPosition}" KeySpline="1.0, 0.0, 1.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="ProgressBarTrack"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0" KeySpline="1.0, 0.0, 1.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationEndPosition}" KeySpline="1.0, 0.0, 1.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>

                            <Storyboard x:Key="UpdatingErrorStoryboard">
                                <ColorAnimation
                                    Storyboard.TargetName="DeterminateProgressBarIndicator"
                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                    To="{DynamicResource ProgressBarErrorForegroundColor}"
                                    Duration="0:0:0.0" />
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ProgressBarTrack" Storyboard.TargetProperty="Fill">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding Background}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="IndeterminateStoryboard" RepeatBehavior="Forever">
                                <!--#region Setters-->
                                <DoubleAnimation Storyboard.TargetName="IndeterminateProgressBarIndicator" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="IndeterminateProgressBarIndicator2" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="ProgressBarTrack" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <!--#endregion-->
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="IndeterminateProgressBarIndicator" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <DiscreteDoubleKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationStartPosition}" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:1.5" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationEndPosition}" KeySpline="0.4, 0.0, 0.6, 1.0" />
                                    <DiscreteDoubleKeyFrame KeyTime="0:0:2" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationEndPosition}" />
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="IndeterminateProgressBarIndicator2" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <DiscreteDoubleKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationStartPosition}" />
                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0.75" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationStartPosition}" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:2" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationEndPosition}" KeySpline="0.4, 0.0, 0.6, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="IndeterminateErrorStoryboard">
                                <!--#region Setters-->
                                <DoubleAnimation Storyboard.TargetName="IndeterminateProgressBarIndicator" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="DeterminateProgressBarIndicator" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="IndeterminateProgressBarIndicator2" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="ProgressBarTrack" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <!--#endregion-->
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="IndeterminateProgressBarIndicator2"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.167" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationEndPosition}" KeySpline="1.0, 1.0, 0.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.167" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationStartPosition}" KeySpline="1.0, 1.0, 0.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationMidPosition}" KeySpline="0.0, 0.0, 0.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                                <ColorAnimation
                                    Storyboard.TargetName="IndeterminateProgressBarIndicator2"
                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                    To="{DynamicResource ProgressBarErrorForegroundColor}"
                                    Duration="0:0:0.167">
                                </ColorAnimation>
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="ProgressBarTrack"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationStartPosition}" KeySpline="0.0, 0.0, 0.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.75" Value="0" KeySpline="0.0, 0.0, 0.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="ErrorStoryboard">
                                <ColorAnimation
                                    Storyboard.TargetName="DeterminateProgressBarIndicator"
                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                    To="{DynamicResource ProgressBarErrorForegroundColor}"
                                    Duration="0:0:0.167" />
                            </Storyboard>
                            <Storyboard x:Key="IndeterminatePausedStoryboard">
                                <!--#region Setters-->
                                <DoubleAnimation Storyboard.TargetName="IndeterminateProgressBarIndicator" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="DeterminateProgressBarIndicator" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="IndeterminateProgressBarIndicator2" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="ProgressBarTrack" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                <!--#endregion-->
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="IndeterminateProgressBarIndicator2"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.167" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationEndPosition}" KeySpline="1.0, 1.0, 0.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.167" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationStartPosition}" KeySpline="1.0, 1.0, 0.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ContainerAnimationMidPosition}" KeySpline="0.0, 0.0, 0.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                                <ColorAnimation
                                    Storyboard.TargetName="IndeterminateProgressBarIndicator2"
                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                    To="{DynamicResource ProgressBarPausedForegroundColor}"
                                    Duration="0:0:0.167">
                                </ColorAnimation>
                                <DoubleAnimationUsingKeyFrames
                                    Storyboard.TargetName="ProgressBarTrack"
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="0:0:0" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.Container2AnimationStartPosition}" KeySpline="0.0, 0.0, 0.0, 1.0" />
                                    <SplineDoubleKeyFrame KeyTime="0:0:0.75" Value="0" KeySpline="0.0, 0.0, 0.0, 1.0" />
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="PausedStoryboard">
                                <ColorAnimation
                                    Storyboard.TargetName="DeterminateProgressBarIndicator"
                                    Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                    To="{DynamicResource ProgressBarPausedForegroundColor}"
                                    Duration="0:0:0.167" />
                            </Storyboard>
                        </Grid.Resources>

                        <VisualStateManager.CustomVisualStateManager>
                            <root:SimpleVisualStateManager />
                        </VisualStateManager.CustomVisualStateManager>

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">

                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="Updating" To="Determinate" Storyboard="{StaticResource UpdatingToDeterminateStoryboard}" />
                                    <VisualTransition From="UpdatingError" To="Error" Storyboard="{StaticResource UpdatingErrorToErrorStoryboard}" />
                                    <VisualTransition From="Paused" To="Determinate" Storyboard="{StaticResource PausedToDeterminateStoryboard}" />
                                    <VisualTransition From="Error" To="Determinate" Storyboard="{StaticResource ErrorToDeterminateStoryboard}" />
                                    <VisualTransition From="Indeterminate" To="Determinate">
                                        <Storyboard>
                                            <Storyboard>
                                                <animation:FadeInThemeAnimation TargetName="IndeterminateProgressBarIndicator" />
                                                <animation:FadeInThemeAnimation TargetName="IndeterminateProgressBarIndicator2" />
                                                <animation:FadeInThemeAnimation TargetName="ProgressBarTrack" />
                                            </Storyboard>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="IndeterminatePaused" To="Indeterminate" Storyboard="{StaticResource IndeterminatePausedToIndeterminateStoryboard}" />
                                    <VisualTransition From="IndeterminateError" To="Indeterminate" Storyboard="{StaticResource IndeterminateErrorToIndeterminateStoryboard}" />
                                </VisualStateGroup.Transitions>

                                <VisualState x:Name="Determinate" />
                                <VisualState x:Name="Updating" />
                                <VisualState x:Name="UpdatingError" Storyboard="{StaticResource UpdatingErrorStoryboard}" />
                                <VisualState x:Name="Error" Storyboard="{StaticResource ErrorStoryboard}" />
                                <VisualState x:Name="Paused" Storyboard="{StaticResource PausedStoryboard}" />
                                <VisualState x:Name="Indeterminate" Storyboard="{StaticResource IndeterminateStoryboard}" />
                                <VisualState x:Name="IndeterminateError" Storyboard="{StaticResource IndeterminateErrorStoryboard}" />
                                <VisualState x:Name="IndeterminatePaused" Storyboard="{StaticResource IndeterminatePausedStoryboard}" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Border
                            x:Name="ProgressBarRoot"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}"
                            CornerRadius="{TemplateBinding CornerRadius}">

                            <Border Clip="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.ClipRect}">
                                <Grid Height="{TemplateBinding MinHeight}">

                                    <Rectangle
                                        x:Name="ProgressBarTrack"
                                        Fill="{TemplateBinding Background}"
                                        Height="{StaticResource ProgressBarTrackHeight}"
                                        Width="{TemplateBinding Width}"
                                        VerticalAlignment="Center"
                                        RadiusX="{Binding Source={StaticResource ProgressBarTrackCornerRadius}, Converter={StaticResource TopLeftCornerRadiusDoubleValueConverter}}"
                                        RadiusY="{Binding Source={StaticResource ProgressBarTrackCornerRadius}, Converter={StaticResource BottomRightCornerRadiusDoubleValueConverter}}">
                                        <Rectangle.RenderTransform>
                                            <TranslateTransform/>
                                        </Rectangle.RenderTransform>
                                    </Rectangle>

                                    <Rectangle
                                        x:Name="DeterminateProgressBarIndicator"
                                        Margin="{TemplateBinding Padding}"
                                        Fill="{TemplateBinding Foreground}"
                                        HorizontalAlignment="Left"
                                        RadiusX="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource TopLeftCornerRadiusDoubleValueConverter}}"
                                        RadiusY="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BottomRightCornerRadiusDoubleValueConverter}}">
                                        <Rectangle.RenderTransform>
                                            <TranslateTransform />
                                        </Rectangle.RenderTransform>
                                    </Rectangle>

                                    <Rectangle
                                        x:Name="IndeterminateProgressBarIndicator"
                                        Margin="{TemplateBinding Padding}"
                                        Fill="{TemplateBinding Foreground}"
                                        HorizontalAlignment="Left"
                                        Opacity="0"
                                        RadiusX="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource TopLeftCornerRadiusDoubleValueConverter}}"
                                        RadiusY="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BottomRightCornerRadiusDoubleValueConverter}}">
                                        <Rectangle.RenderTransform>
                                            <TranslateTransform />
                                        </Rectangle.RenderTransform>
                                    </Rectangle>

                                    <Rectangle
                                        x:Name="IndeterminateProgressBarIndicator2"
                                        Margin="{TemplateBinding Padding}"
                                        Fill="{TemplateBinding Foreground}"
                                        HorizontalAlignment="Left"
                                        Opacity="0"
                                        RadiusX="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource TopLeftCornerRadiusDoubleValueConverter}}"
                                        RadiusY="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BottomRightCornerRadiusDoubleValueConverter}}">
                                        <Rectangle.RenderTransform>
                                            <TranslateTransform />
                                        </Rectangle.RenderTransform>
                                    </Rectangle>
                                </Grid>
                            </Border>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>