From 18eefa0037aae483b852b44505c72c9ecaab33e2 Mon Sep 17 00:00:00 2001 From: Yimeng Wu Date: Mon, 21 Dec 2020 01:15:26 +0800 Subject: [PATCH] Progressbar: Fix issue with color changing when updating value while error state (microsoft/microsoft-ui-xaml#3201) --- ModernWpf/Controls/Primitives/BindingProxy.cs | 28 +++++++++ ModernWpf/ProgressBar/ProgressBar.cs | 57 ++++++++++++------- ModernWpf/ProgressBar/ProgressBar.xaml | 39 ++++++++++--- 3 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 ModernWpf/Controls/Primitives/BindingProxy.cs diff --git a/ModernWpf/Controls/Primitives/BindingProxy.cs b/ModernWpf/Controls/Primitives/BindingProxy.cs new file mode 100644 index 00000000..5cca8369 --- /dev/null +++ b/ModernWpf/Controls/Primitives/BindingProxy.cs @@ -0,0 +1,28 @@ +using System.Windows; + +namespace ModernWpf.Controls.Primitives +{ + public class BindingProxy : Freezable + { + #region Value + + public static readonly DependencyProperty ValueProperty = + DependencyProperty.Register( + nameof(Value), + typeof(object), + typeof(BindingProxy)); + + public object Value + { + get => GetValue(ValueProperty); + set => SetValue(ValueProperty, value); + } + + #endregion + + protected override Freezable CreateInstanceCore() + { + return new BindingProxy(); + } + } +} diff --git a/ModernWpf/ProgressBar/ProgressBar.cs b/ModernWpf/ProgressBar/ProgressBar.cs index e133d1c0..a39ef3c9 100644 --- a/ModernWpf/ProgressBar/ProgressBar.cs +++ b/ModernWpf/ProgressBar/ProgressBar.cs @@ -270,31 +270,38 @@ private void OnShowErrorPropertyChanged(DependencyPropertyChangedEventArgs args) private void UpdateStates(bool useTransitions = true) { - if (ShowError && IsIndeterminate) - { - VisualStateManager.GoToState(this, s_IndeterminateErrorStateName, useTransitions); - } - else if (ShowError) - { - VisualStateManager.GoToState(this, s_ErrorStateName, useTransitions); - } - else if (ShowPaused && IsIndeterminate) - { - VisualStateManager.GoToState(this, s_IndeterminatePausedStateName, useTransitions); - } - else if (ShowPaused) - { - VisualStateManager.GoToState(this, s_PausedStateName, useTransitions); - } - else if (IsIndeterminate) + if (IsIndeterminate) { + if (ShowError) + { + VisualStateManager.GoToState(this, s_IndeterminateErrorStateName, true); + } + else if (ShowPaused) + { + VisualStateManager.GoToState(this, s_IndeterminatePausedStateName, true); + } + else + { + VisualStateManager.GoToState(this, s_IndeterminateStateName, true); + } UpdateWidthBasedTemplateSettings(); - VisualStateManager.GoToState(this, s_IndeterminateStateName, useTransitions); } - else if (!IsIndeterminate) + else { - VisualStateManager.GoToState(this, s_DeterminateStateName, useTransitions); + if (ShowError) + { + VisualStateManager.GoToState(this, s_ErrorStateName, true); + } + else if (ShowPaused) + { + VisualStateManager.GoToState(this, s_PausedStateName, true); + } + else + { + VisualStateManager.GoToState(this, s_DeterminateStateName, true); + } } + } private void SetProgressBarIndicatorWidth() @@ -315,7 +322,14 @@ private void SetProgressBarIndicatorWidth() // Adds "Updating" state in between to trigger RepositionThemeAnimation Visual Transition // in ProgressBar.xaml when reverting back to previous state - VisualStateManager.GoToState(this, s_UpdatingStateName, true); + if (ShowError) + { + VisualStateManager.GoToState(this, s_UpdatingWithErrorStateName, true); + } + else + { + VisualStateManager.GoToState(this, s_UpdatingStateName, true); + } if (IsIndeterminate) { @@ -473,5 +487,6 @@ private void ReapplyIndeterminateStoryboard() const string s_IndeterminatePausedStateName = "IndeterminatePaused"; const string s_DeterminateStateName = "Determinate"; const string s_UpdatingStateName = "Updating"; + const string s_UpdatingWithErrorStateName = "UpdatingError"; } } diff --git a/ModernWpf/ProgressBar/ProgressBar.xaml b/ModernWpf/ProgressBar/ProgressBar.xaml index b735463f..9beeb011 100644 --- a/ModernWpf/ProgressBar/ProgressBar.xaml +++ b/ModernWpf/ProgressBar/ProgressBar.xaml @@ -28,14 +28,20 @@ x:Name="LayoutRoot" SnapsToDevicePixels="True"> + - - - - - - - + + + + + + + + + + @@ -87,7 +93,7 @@ Value="{DynamicResource SystemAccentColor}" KeyTime="0:0:2.75" /> + @@ -190,6 +197,20 @@ + + + + + +