2013-01-10 23 views
5

WPF'de bir ProgressBar'ın değer özelliğini veritabanına veriyorum. ProgressBar değerinin databound int özelliğini artırmak için ayarlanmış bir düğme var. Düğmeye bastığımda, ProgressBar'ın Değerini 1'den 100'e kadar saymalı. Ancak, ... ... işe yaramıyor gibi görünmüyor ve ne yaptığımı bilmiyorum. İşte benim XAML ... BuradaDeğer veri tabanına sahip WPF ProgressBar

<Window x:Class="ProgressBarExample2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="250" Width="400" Background="WhiteSmoke"> 
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> 
    <Button Name="goButton" Height="30" Width="50" Margin="0,10,0,50" Click="goButton_Click">GO!</Button> 
    <ProgressBar Name="progressBar" Width="300" Height="30" Value="{Binding Percent, UpdateSourceTrigger=PropertyChanged}" /> 
</StackPanel> 

ve arkasında benim kodu ...

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    #region INotifyPropertyChanged 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void NotifyPropertyChange(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 

    private int percent = 0; 
    public int Percent 
    { 
     get { return this.percent; } 
     set 
     { 
      this.percent = value; 
      NotifyPropertyChange("Percent"); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 


    private void goButton_Click(object sender, RoutedEventArgs e) 
    { 
     for (Percent = 0; Percent <= 100; Percent++) 
     { 
      Thread.Sleep(50); 
     } 
    } 
} 
+0

belki butonuna bir zamanlayıcısı sayacını başlatmak tıklayın eklemeyi deneyebilirsiniz tıklatma olayı kullanabilirsiniz. Zamanlayıcı geri çağırmada Yüzde- uygun veri bağlama ile bu sadece çalışmalıdır. Zamanlayıcı geri bildirimleri arka plan iş parçacığınızdır, böylece ana iş parçacığınız ilerleme çubuğu – Sarang

cevap

1

hiçbir kod yoktur olan (gönderilen) penceresinin DataContext'i setleri (veya StackPanel).

Nedeni hakkında emin olmak için, Ciltleme hataları için Çıktı penceresini izleyin.


Ve ek olarak

,

private void goButton_Click(object sender, RoutedEventArgs e) 
{ 
    for (Percent = 0; Percent <= 100; Percent++) 
    { 
     Thread.Sleep(50); 
    } 
} 

Bu bloklar mesajı işleme uygulama 5 saniye 'tepkisiz' olacak böylece. Hiçbir girdi işleme ve ekran güncellemesi yapılmayacaktır. Etkinlik odaklı bir GUI'de yoğun bir döngü iyi değildir.

Bu kodu bir Backgroundworker'a taşıyın veya bir Zamanlayıcı kullanın.

+0

Ahh güncellemeyi ücretsiz olarak güncellediler. Bunu unuttum şey ... Tamam, bu düzeltildi ... ama değeri gerçek zamanlı olarak güncellemedi ... şimdi uyku süresini uzatsam bile yüzde 0'dan yüzde 100'e fırlar ... – user1451495

+0

Bunun için bir cevap da ekledi. –

6

Thread.Sleep, UI iş parçacığını engelliyor ve ilerleme çubuğunun animasyonunu durduruyor.

UI iş parçacığı engellenmeden yürütmeyi duraklatmak için aşağıdakileri kullanabilirsiniz. Wait(50)

EDIT'e sizin Thread.Sleep(50) çağrıyı değiştir: Kaldırılan temsilci veri bağlama olmadan başka çözümler vardır

/// <summary> 
/// Stop execution for a specific amount of time without blocking the UI 
/// </summary> 
/// <param name="interval">The time to wait in milliseconds</param> 
public static void Wait(int interval) 
{ 
    ExecuteWait(() => Thread.Sleep(interval)); 
} 

public static void ExecuteWait(Action action) 
{ 
    var waitFrame = new DispatcherFrame(); 

    // Use callback to "pop" dispatcher frame 
    IAsyncResult op = action.BeginInvoke(dummy => waitFrame.Continue = false, null); 

    // this method will block here but window messages are pumped 
    Dispatcher.PushFrame(waitFrame); 

    // this method may throw if the action threw. caller's responsibility to handle. 
    action.EndInvoke(op); 
} 
+0

UI'nin neden donmuş olduğunu anlamaya çalışan bütün bir gün boşa harcadı. Beni yenile. – NotAgain

0

. Bir Temsilciye

private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);

beyan ve düğme

private void goButton_Click(object sender, RoutedEventArgs e) 
     { 
      //Configure the ProgressBar 
      progressBar.Minimum = 0; 
      progressBar.Maximum = 100; 
      progressBar.Value = 0; 

      //Stores the value of the ProgressBar 
      double value = 0; 

      //Create a new instance of our ProgressBar Delegate that points 
      // to the ProgressBar's SetValue method. 
      UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar.SetValue); 

      //Tight Loop: Loop until the ProgressBar.Value reaches the max 
      do 
      { 
       value += 1; 

       /*Update the Value of the ProgressBar: 
        1) Pass the "updatePbDelegate" delegate that points to the ProgressBar1.SetValue method 
        2) Set the DispatcherPriority to "Background" 
        3) Pass an Object() Array containing the property to update (ProgressBar.ValueProperty) and the new value */ 
       Dispatcher.Invoke(updatePbDelegate, 
        System.Windows.Threading.DispatcherPriority.Background, 
        new object[] { ProgressBar.ValueProperty, value }); 

      } 
      while (progressBar.Value != progressBar.Maximum); 
     } 
İlgili konular