当WPF进度条中的Indeterminate =“True”时,如何显示进度?

时间:2010-05-16 18:01:17

标签: wpf wpf-controls

这一直是所有问题中最简单的问题,但我似乎无法弄明白。我有进度条,如何让它显示进度。我该如何开始呢?

<ProgressBar x:Name="ProgressUpload" Margin="5" IsIndeterminate="True" ></ProgressBar>

7 个答案:

答案 0 :(得分:22)

如果将IsIndeterminate设置为True,则进度具有某些内容正在进行但您无法确定确切持续时间的含义。因此,我只能告诉您将其设置为false并在其“标准”行为中使用进度条。

答案 1 :(得分:16)

如果你试图让进度条开始,简单地说,但是作为一个不确定的条,你必须在准备好时将属性IsIndeterminate设置为true,并在完成时将其设置为false。

换句话说:

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

为了向您提供有关您为什么要这样做的上下文,请查看以下伪代码:

//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;
}

让我第一个说我不确定这是否是这个属性的预期用法,但我可以说它肯定有效。

答案 2 :(得分:1)

显然在某些环境中,必须明确设置高度才能运行不确定的动画,而在其他环境中则不需要高度。

答案 3 :(得分:0)

在初始化期间(即XAML中的UI设计器,或代码端的构造函数),不要将其设置为IsIndeterminate。如果您这样做,则动画将无法启动。将其设置在“已加载”内。事件处理程序。

我会在XAML方面IsIndeterminate = 'False',然后在Window_Loaded事件中设置:

myProgressBar.IsIndeterminate = true;

答案 4 :(得分:0)

问题的一个可能的解决方法是简单地显示或隐藏ProgressBar控件:

progressBar.Visibility = Visibility.Visible;

progressBar.Visibility = Visibility.Collapsed;

答案 5 :(得分:0)

这与上面的@dyslexicanaboko没有什么区别,但是你可以快速轻松地进行可以控制的演示:

在XAML中:

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

在代码背后:

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

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

单击“开始处理”按钮后,动画将开始并继续,直到单击“停止处理”按钮。这应该是显而易见的,但是IsIndeterminate选项不是很好的UI实践;更好地实际更新价值,但对于那些希望在行动中看到这一点的人......

答案 6 :(得分:0)

还要确保CacheMode =&#34; BitmapCache&#34;未设置为您的页面 - 否则动画将无法运行。它只显示缓存的位图...

相关问题