我試圖綁定WPF中ProgressBar的value屬性。我有一個按鈕,用於爲ProgressBar的Value增加數據綁定int屬性。當我按下按鈕時,它應該使ProgressBar的值從1增加到100.但是...它似乎並沒有工作,我不知道我在做什麼錯誤。這是我的XAML ...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>
,這裏是我的背後...代碼
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);
}
}
}
可以或許嘗試添加按鈕一timer-點擊啓動計時器的Click事件中使用它。在定時器回調增量中,具有正確的數據綁定的Percent-應該可以工作。定時器回調是在後臺線程,所以你的主線程是免費更新進度條 – Sarang