【问题标题】:Why can't I set the value of a ProgressBar?为什么我不能设置 ProgressBar 的值?
【发布时间】:2011-02-14 02:39:30
【问题描述】:

朋友们,感谢您的宝贵时间! WPF 中的以下代码存在问题。我使用 ProgressBar 和 Animation 来显示值更改的进度。当 Animation 完成后,我尝试将值重置为 0 ,以便 ProgressBar 可以从 0 开始新的 Animation再次变为100。但结果是我无法将值设置为0,似乎无论我如何尝试都将永远是100!!!!!!! 能否请您给我一些想法,谢谢!

        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);

【问题讨论】:

    标签: wpf progress-bar


    【解决方案1】:

    请参阅this article


    总结:动画后设置值的三种方式。

    1. 将动画的 FillBehavior 属性设置为 Stop
    2. 删除整个情节提要。 (不适用,因为您没有情节提要)
    3. 从单个属性中删除动画。

    (1)设置动画的填充行为停止:

    da.FillBehavior = FillBehavior.Stop;
    

    (3) 在设置新值之前调用这个来移除动画:

    pbStatus.BeginAnimation(ProgressBar.ValueProperty, null);
    

    【讨论】:

    • 非常感谢!真的很有帮助!解决这个问题的方法可能是停止动画或者移除它。
    • 是的,当您回答时,我添加了所需的明确操作。
    • 顺便说一句,像这样动画进度条并不是一个好主意,因为它没有说明实际进度;您应该通过将IsIndeterminate 设置为true 来使用不确定的进度条。此外,如果这是您正在寻找的答案,您可以通过单击左侧的复选标记大纲来接受它。
    【解决方案2】:

    来自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);
    
    }  
    

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 2017-10-31
      • 2011-02-07
      相关资源
      最近更新 更多