How to reset WPF progress bar?
-
20-09-2019 - |
Question
I'm testing a WPF progress bar and trying to reset it to its initial state, but it doesn't work.
Duration duration = new Duration(TimeSpan.FromSeconds(1));
DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
myProgress.IsIndeterminate = true;
myProgress.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
myProgress.Value = 0;
Before animation, the progress bar is static (no animation). After animation, the progress bar color is now light gray (lighter than before) with a brightening flash effect.
When commenting duration and double animation, the progress bar remains static. I see there's something to do with this double animation.
//Duration duration = new Duration(TimeSpan.FromSeconds(1));
//DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
myProgress.IsIndeterminate = true;
//myProgress.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
myProgress.Value = 10;
myProgress.Value = 0;
How can I solve this DoubleAnimation issue? What am I missing here?
Solution
Have a look at this thread:
OTHER TIPS
Use this form of the DoubleAnimation instead
DoubleAnimation doubleanimation = new DoubleAnimation(0,200, duration);
Where you explicitly set both a from and too value rather than just a destination value.
Code:
Within the window ctor:
myProgress.Maximum = 100; myProgress.Minimum = 0;
Then say in a button click handler
myProgress.IsIndeterminate = false; //shouldn't really need this PB oddity
myProgress.IsIndeterminate = true;
myProgress.Value = 0;
Duration duration = new Duration(TimeSpan.FromSeconds(1));
DoubleAnimation doubleanimation = new DoubleAnimation(0,200, duration);
myProgress.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
The PB is in a StackPanel, xaml is
<ProgressBar Name="myProgress" Height="20"></ProgressBar>
This was initially tested on XP but see below
For a Win 7 solution please see here