WPF 数据绑定 ProgressBar 未显示进度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19776786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:00:43  来源:igfitidea点击:

WPF Data bound ProgressBar not showing progress

c#wpfdata-bindingprogress-bar

提问by bas

I am having troubles with getting the ProgressBar working.

我在让 ProgressBar 工作时遇到了麻烦。

XAML:

XAML:

    <ProgressBar x:Name="ProgressBar" Value="{Binding Progress, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="37" Margin="10,35,0,0" VerticalAlignment="Top" Width="590"/>

Code behind:

后面的代码:

    ProgressBar.DataContext = progressModel;

IProgressModel:

IProgressModel:

public interface IProgressModel
{
    double Minimum { get; set; }
    double Maximum { get; set; }
    double Progress { get; }
}

Implementation:

执行:

    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++;
            }
        }
    }

And the pretty straight forward PropertyChanged mechanism:

还有非常直接的 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));
    }

The ProgressBar starts "full" and remains that way throughout the process. I have read all related threads on SO, but no luck.

ProgressBar 开始“满”并在整个过程中保持这种状态。我已阅读有关 SO 的所有相关主题,但没有运气。

What am I doing wrong?

我究竟做错了什么?

Thx in advance.

提前谢谢。

回答by Reed Copsey

You need to bind your maximum, too:

您还需要绑定最大值:

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