How do you show progress when IsIndeterminate=“True” in a WPF progress bar?

后端 未结 7 638
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 07:50

This has to be the simplest question of all, but I cannot seem to figure it out. I have the progress bar in place. How do I make it show progress? How do I start the thing?<

相关标签:
7条回答
  • 2020-12-31 08:23

    A possible workaround of the problem is to simply show or hide the ProgressBar control:

    progressBar.Visibility = Visibility.Visible;
    
    progressBar.Visibility = Visibility.Collapsed;
    
    0 讨论(0)
  • 2020-12-31 08:27

    Apparently in some environments Height has to be set explicitly for indeterminate animation to run, while on others it is not needed.

    0 讨论(0)
  • 2020-12-31 08:27

    If you set IsIndeterminate to True, the progress has the meaning that something is in progress but you cannot determine the exact duration. So, I can only tell you to set it to false and to use the progress bar in its "standard" behavior.

    0 讨论(0)
  • 2020-12-31 08:36

    Also make sure that CacheMode="BitmapCache" is not set to your page - otherwise the animation will not run. It just displays the cached bitmap...

    0 讨论(0)
  • 2020-12-31 08:37

    Simply put if you are trying to make the progress bar start, but as an indeterminate bar, then you must set the property IsIndeterminate to true when ready and to false when finished.

    So in other words:

    pbar.IsIndeterminate = true; //This starts your bar's animation
    pbar.IsIndeterminate = false; //This stops your bar's animation
    

    To give you context as to why you would want to do it this way look at the following psuedo code:

    //Some method that is going to start something that is going to take a while
    public void StartLongRunningProcess()
    {
        //Make a call to a web service asynchronously etc...
    
        //Start the animation for your progress bar
        pbar.IsIndeterminate = true;
    }
    
    //The method (delegate) that handles the result, usually from an event.
    //This method will handle the result of the asynchronous call 
    public void HandlerForLongRunningProcess()
    {
        //Do stuff with result from your asynchronous web service call etc...
    
        //Stop the animation for your progress bar
        pbar.IsIndeterminate = false;
    }
    

    Let me be the first to say that I am not sure if this is the intended usage of this property, but I can say it definitely works.

    0 讨论(0)
  • 2020-12-31 08:40

    This is no real difference than @dyslexicanaboko above, but it is quick and easy to do for a demonstration you can control:

    In XAML:

    <Button Content="Start Process" HorizontalAlignment="Center" Click="StartAProcess"/>
    <Button Content="Stop Process" HorizontalAlignment="Center" Click="StopAProcess"/>
    <ProgressBar Name="pBar1" Height="20"/>
    

    In code behind:

    Public Sub StartAProcess()
      pBar1.IsIndeterminate = True
    End Sub
    
    Public Sub StopAProcess()
      pBar1.IsIndeterminate = False
    End Sub
    

    Upon clicking the Start Process button the animation will start and continue until the Stop Process button is clicked. It should be obvious, but the IsIndeterminate option is not good UI practice; better to actually update the value, but for those who want to see this in action...

    0 讨论(0)
提交回复
热议问题