简体   繁体   中英

WPF ProgressBar doesn't update?

I am trying to update my progressbar by using data bindings. The XAML file contains the progressbar:

<ProgressBar Height="23" Name="progressBar" VerticalAlignment="Bottom" Margin="207,444,0,0" Minimum="0" Maximum="{Binding ProgressBarMax}" Value="{Binding ProgressBarValue}" />

My relevant C# class contains the getter & setter:

    private int progressBarMax;
    public int ProgressBarMax
    {
        get 
        {
            if (this.progressBarMax == 0)
                this.progressBarMax = 1;
            return this.progressBarMax; 
        }
        set 
        {
            this.progressBarMax = value; 
        }
    }

    private int progressBarValue;
    public int ProgressBarValue
    {
        get 
        { 
            return progressBarValue; 
        }
        set 
        { 
            progressBarValue = value; 
        }
    }

In my "update" method the maximum is being set. For example like this.progressBarMax = 100; . In a loop the progressbar value is getting the value += 1. To see the updates I used Application.DoEvents(), later I will implement threads. The data binding has to be correct, because I have other components that work fine.

So why doesn't my progressbar update?

Thank you for your help.

I was hoping to actually add a comment to the original post, but I will have to settle for a question regarding the original entered through the answer text bax. Can you post your code as a unit? That is, all the code you needed to add to complete the binding? I am having trouble getting all the pieces together to do a data binding for my maximum and value for my progress bar and it may help quite a bit.

You need to implement a way to let your ProgressBar be notified whenever ProgressBarValue changes. Have a look at the INotifyPropertyChanged interface.

What about INotifyPropertyChanged to make your UI controls detect updated values?

//daniel

I found the mistake in my code. Unfortunately I changed my private members, not the public properties. So when using this.ProgressBarValue += 1; instead of this.progressBarValue += 1; everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM