如何为WPF ProgressBar实现INotifyPropertyChanged

时间:2014-12-17 11:36:39

标签: wpf progress-bar inotifypropertychanged

我正在阅读有关如何为进度条实现INotifyPropertyChanged的教程,我觉得我有点困惑。

这是我的XAML代码段:

<ProgressBar Name="progressBar" Height="24" IsIndeterminate="{Binding IsIndeterminate}" Minimum="{Binding Minimum}" Maximum="{Binding Maximum}" Value="{Binding ProgressValue}"/>

这是我的代码隐藏代码段:

        public partial class MainWindow : System.Windows.Window {
            public bool IsInderteminate { get; set; }
            public double Minimum { get; set; }
            public double Maximum { get; set; }
            public double ProgressValue { get; set; }


            public MainWindow() {
                InitializeComponent();
                this.progressBar.DataContext = this;
                this.IsInderteminate = false;
            }


      private void btnLoadPremiumDetail_Click(object sender, RoutedEventArgs e) {
                    this.IsInderteminate = true;
                    // I do my work here
                    this.IsInderteminate = false;
            }


            private void _ValidateProcurementDetail() {

                for (int i = 0; i < rowCount; i++) {
                    this.ProgressValue += 1;
                    //I do work here
                }
    }
}

我如何实现INotifyPropertyChanged,以便每次设置ProgressValue或IsIndeterminate时我的progressBar都会更新?

1 个答案:

答案 0 :(得分:1)

这里该怎么做:

 public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.progressBar.DataContext = this;
        this.IsInderteminate = false;
    }

    private bool _isInderteminate = false;

    public bool IsInderteminate
    {
        get { return _isInderteminate; }

        set
        {
            if (_isInderteminate == value)
            {
                return;
            }

            _isInderteminate = value;
            OnPropertyChanged();
        }
    }

    private double _minimum;

    public double Minimum
    {
        get { return _minimum; }

        set
        {
            if (_minimum == value)
            {
                return;
            }

            _minimum = value;
            OnPropertyChanged();
        }
    }

    private double _maximum;

    public double Maximum
    {
        get { return _maximum; }

        set
        {
            if (_maximum == value)
            {
                return;
            }

            _maximum = value;
            OnPropertyChanged();
        }
    }

    private double _progressValue;

    public double ProgressValue
    {
        get { return _progressValue; }

        set
        {
            if (_progressValue == value)
            {
                return;
            }

            _progressValue = value;
            OnPropertyChanged();
        }
    }

    private void btnLoadPremiumDetail_Click(object sender, RoutedEventArgs e)
    {
        this.IsInderteminate = true;
        // I do my work here
        this.IsInderteminate = false;
    }


    private void _ValidateProcurementDetail()
    {

        for (int i = 0; i < rowCount; i++)
        {
            this.ProgressValue += 1;
            //I do work here
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

在Xaml中添加Binding Mode=TwoWay,您也可以直接从Xaml设置DataContext,这样就可以获得IntelliSense:

DataContext="{Binding RelativeSource={RelativeSource Self}}"