5

我无法让 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 的所有相关主题,但没有运气。

我究竟做错了什么?

提前谢谢。

4

1 回答 1

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"/>
于 2013-11-04T20:48:11.953 回答