WPF数据绑定ProgressBar未显示进度

时间:2013-11-04 20:45:13

标签: c# wpf data-binding progress-bar

我在使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上的所有相关主题,但没有运气。

我做错了什么?

提前谢谢。

1 个答案:

答案 0 :(得分:6)

你也需要绑定你的最大值:

    <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"/>