我在使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));
}
ProgressBar开始“已满”,并在整个过程中保持这种状态。我已经阅读了SO上的所有相关主题,但是没有运气。
我究竟做错了什么?
提前谢谢。
最佳答案
您还需要绑定(bind)最大值:
<ProgressBar x:Name="ProgressBar"
Value="{Binding Progress}"
Maximum="{Binding Maximum}"
Minimum="{Binding Minimum}"
HorizontalAlignment="Left" Height="37" Margin="10,35,0,0"
VerticalAlignment="Top" Width="590"/>
关于c# - WPF数据绑定(bind)的ProgressBar不显示进度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19776786/