Claw
Claw

Reputation: 474

Why can't I set the value of a ProgressBar?

friends,thank you for your time! There's a problem in the codes below in WPF.I use a ProgressBar and a Animation which show the progress of value changes.When the Animation finished,I try to reset the value to 0 ,so that the ProgressBar can begin a new Animation from 0 to 100 again.But the result is that I can't set the value to 0,it seems that it would be 100 forever no matter how I tried!!!!! Could you please give me some ideas,thank you!

        pbStatus.Value = 0;//Promblem!! pbStatus is a ProgressBar
        Duration dr = new Duration(TimeSpan.FromSeconds(2));
        DoubleAnimation da = new DoubleAnimation(100, dr);
        pbStatus.IsIndeterminate = false;
        pbStatus.Visibility = Visibility.Visible;
        pbStatus.BeginAnimation(ProgressBar.ValueProperty, da);

Upvotes: 1

Views: 2351

Answers (2)

brunnerh
brunnerh

Reputation: 185225

Please see this article.


Summary: There are three ways to set a value after an animation.

  1. Set the animation's FillBehavior property to Stop
  2. Remove the entire Storyboard. (Not applicable since you have no storyboard)
  3. Remove the animation from the individual property.

(1) Set the fill behaviour of the animation to stop:

da.FillBehavior = FillBehavior.Stop;

(3) Remove the animation by calling this before setting the new value:

pbStatus.BeginAnimation(ProgressBar.ValueProperty, null);

Upvotes: 4

abramlimpin
abramlimpin

Reputation: 5087

From this article:

private void CreateDynamicProgressBarControl()
{
    ProgressBar PBar2 = new ProgressBar();
    PBar2.IsIndeterminate = false;
    PBar2.Orientation = Orientation.Horizontal;
    PBar2.Width = 100;
    PBar2.Height = 10;
    Duration duration = new Duration(TimeSpan.FromSeconds(10));
    DoubleAnimation doubleanimation = new DoubleAnimation(100.0, duration);
    PBar2.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
    SBar.Items.Add(PBar2);

}  

Upvotes: 1

Related Questions