如何为WPF ProgressBar实现INotifyPropertyChanged [英] How to implement INotifyPropertyChanged for WPF ProgressBar

查看:121
本文介绍了如何为WPF ProgressBar实现INotifyPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am going through the tutorials on how to implement INotifyPropertyChanged for a progressbar and I think i am a bit confused.

这是我的XAML代码段:

This is my XAML code snippet:

<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?

How do i implement the INotifyPropertyChanged so that my progressBar gets updated everytime I set the ProgressValue or IsIndeterminate?

推荐答案

此处操作方法:

 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:

in the Xaml add the Binding Mode=TwoWay, you could also set the DataContext Directly from the Xaml so you can get IntelliSense like so:

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

这篇关于如何为WPF ProgressBar实现INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆