2010-05-16 158 views
10

這是所有問題中最簡單的問題,但我似乎無法弄清楚。我有進度條,我如何讓它顯示進度。我如何開始這件事?如何在WPF進度條中Indeterminate =「True」時顯示進度?

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

爲什麼你首先有'IsIndeterminate =「True」'? – 2010-05-16 21:57:01

回答

21

如果設置IsIndeterminate爲True,進度有一些正在進行中的意思,但你不能確定確切的時間。所以,我只能告訴你將它設置爲false,並在其「標準」行爲中使用進度條。

+0

我正在戰鬥的問題是如何讓它開始動畫。我有定義的欄,並且是IsIndeterminate =「True」。我如何觸發酒吧做它的事情? – user38349 2010-05-17 01:29:02

+9

我一直在尋找的技巧是我必須設置最小值和最大值以及高度。一旦我設置它顯示。 – user38349 2010-05-17 13:08:06

15

簡單地說,如果您試圖使進度條開始,但作爲一個不確定的欄,那麼您必須在準備好時將屬性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; 
} 

讓我成爲第一個說我不確定這是否屬於這個屬性的預期用法,但我可以說它絕對有效。

+0

我很困惑如何使用MVVM將異步調用添加到我的進度條:http://stackoverflow.com/questions/25021838/indeterminate-progress-bar – 2014-07-30 18:38:02

1

顯然,在某些環境中,高度必須明確設置爲不確定的動畫運行,而在其他人不需要。

0

在擁有它的窗口的初始化過程中(即XAML中的UI設計器或代碼側的構造函數)不要將它設置爲IsIndeterminate。如果你這樣做,動畫將不會啓動。將其設置在「Loaded」事件處理程序中。

我會對XAML側IsIndeterminate = 'False',然後在Window_Loaded事件,設置:

myProgressBar.IsIndeterminate = true; 
0

問題的可能的解決方法是簡單地顯示或隱藏進度控制:

progressBar.Visibility = Visibility.Visible; 

progressBar.Visibility = Visibility.Collapsed; 
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練習;更好地實際更新值,但對於那些想要看到這一點的人...

0

還要確保CacheMode =「BitmapCache」未設置爲您的頁面 - 否則動畫將無法運行。它只是顯示緩存的位圖...