我有一个进度条,它的值绑定到一个属性:
<ProgressBar x:Name="progressBar"
Margin="0,2,0,0"
Height="20"
Value="{Binding CompassLogLoadPercent}"
Foreground="Blue"
Visibility="{Binding CompassLogLoadCompleted,
Converter={StaticResource BooleanToVisibilityConverter}}"
ToolTip="Loading">
</ProgressBar>
和财产:
public double CompassLogLoadPercent {
get { return _compassLogLoadPercent; }
private set {
if (value != _compassLogLoadPercent) {
_compassLogLoadPercent = value;
NotifyPropertyChanged();
}
}
}
并在单独的线程中更新其值:
for (int j = 0; j < lines.Count(); j++) {
...
CompassLogLoadPercent = ((double) j /lines.Count())*100;
}
并且线程是使用 TASK 创建的:
Task.Run(() => { LoadLogFile(fileName); });
为什么进度条没有更新,我应该如何解决这个问题?
更新:更多信息
数据上下文:(我确定数据上下文是正确的)
cLT.progressBar.DataContext = logSession;
和实施INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(
[CallerMemberName] String propertyName = "") {
PropertyChangedEventHandler eventHandler = PropertyChanged;
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}