我有進入工作欄的麻煩。WPF數據綁定ProgressBar沒有顯示進度
XAML:
<ProgressBar x:Name="ProgressBar" Value="{Binding Progress, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="37" Margin="10,35,0,0" VerticalAlignment="Top" Width="590"/>
後面的代碼:
ProgressBar.DataContext = progressModel;
IProgressModel:
public interface IProgressModel
{
double Minimum { get; set; }
double Maximum { get; set; }
double Progress { get; }
}
實現:
private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
Minimum = 0;
Maximum = RenamableFiles.Count;
var i = 0;
foreach (var renamableFile in RenamableFiles)
{
var oldFilename = ReCreateOldFileName(renamableFile);
var renameProposalFilename = CreateNewFileName(renamableFile);
if (oldFilename != null && renameProposalFilename != null && !oldFilename.Equals(renameProposalFilename))
{
// File.Move(oldFilename, renameProposalFilename);
Thread.Sleep(100);
Progress = i++;
}
}
}
和漂亮的直線前進的PropertyChanged機制:
private double _progress;
public double Progress
{
get { return _progress; }
set
{
_progress = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
進度條開始「滿」並保持整個過程的方式。我已閱讀所有相關的線索,但沒有運氣。
我在做什麼錯?
Thx提前。
我猜'WorkerOnDoWork'運行在比UI線程以外的線程。如果是這樣,我不認爲這是非常安全的,因爲你將有兩個線程訪問相同的東西。 – Steve