我正在尝试在 WPF 中对 ProgressBar 的 value 属性进行数据绑定。我设置了一个按钮来增加 ProgressBar 的值的数据绑定 int 属性。当我按下按钮时,它应该使 ProgressBar 的值从 1 增加到 100。但是......它似乎没有工作,我不确定我做错了什么。这是我的 XAML ......
<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);
}
}
}