Tôi đang cố gắng databind thuộc tính giá trị của một ProgressBar trong WPF. Tôi có một nút thiết lập để tăng thuộc tính int databound cho Giá trị của ProgressBar. Khi tôi nhấn nút, nó sẽ làm cho giá trị của ProgressBar tăng từ 1 lên 100. Tuy nhiên ... nó dường như không hoạt động và tôi không chắc mình đang làm gì sai. Dưới đây là XAML của tôi ...Tiến trình tiến hành WPF với Databinding giá trị
<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>
và đây là mã của tôi đằng sau ...
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);
}
}
}
bạn có lẽ có thể thử thêm một timer- vào nút bấm bắt đầu hẹn giờ. Trong gọi lại bộ đếm thời gian tăng phần trăm- với ràng buộc dữ liệu thích hợp, điều này sẽ chỉ hoạt động. Gọi lại hẹn giờ đang ở trong chủ đề nền để chủ đề chính của bạn là miễn phí để cập nhật thanh tiến trình – Sarang