ProgressBar doesn't work properly

kerenshor

New member
Joined
Jan 23, 2016
Messages
4
Programming Experience
1-3
I am using C# WPF MVVM.

The ProgressBar is colored red after a while, but it does not show any movement, and the percentage is not incremented and stays at 0%.
XAML:
<Button Grid.Row="0" Content="Start" Command="{Binding StartCommand}" Style="{StaticResource RotatingButton}"
        FontWeight="Bold" HorizontalAlignment="Left" Width="60" Height="24" Margin="10,10,0,15" />
<ProgressBar Grid.Row="1" Name="pbProgress" Minimum="0" Maximum="{Binding PropMaxValue}" Value="{Binding PropCurrentValue}"
             Height="25" Background="Black" Foreground="Red" Margin="20,20,22.4,0" />
<TextBlock Grid.Row="1" Text="{Binding PropCurrentValuePercent, StringFormat={}{0:0}%}"
           HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="White" FontWeight="Bold" Margin="5" />
ViewModel:
class MainWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        //private double maxValue = 1000000;
        private double maxValue = 50;
        public double PropMaxValue
        {
            get
            {
                return maxValue;
            }
            set
            {
                maxValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("PropMaxValue"));
                }
            }
        }

        private double propCurrentValue;
        public double PropCurrentValue
        {
            get { return propCurrentValue; }
            set
            {
                propCurrentValue = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("PropCurrentValue"));
                }
            }
        }

        private double propCurrentValuePercent;
        public double PropCurrentValuePercent
        {
            get
            {
                //return (int)((PropCurrentValue - 0) / (PropMaxValue - 0) * 100);
                return (double)((PropCurrentValue - 0) / (PropMaxValue - 0) * 100);
            }
            set
            {
                propCurrentValuePercent = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("PropCurrentValuePercent"));
                }
            }
        }

        private bool canExecute = true;
        private ICommand startCommand;

        public ICommand StartCommand
        {
            get
            {
                if (startCommand == null)
                    startCommand = new RelayCommand(RunProgressBar, param => this.canExecute);
                return startCommand;
            }
            set
            {
                startCommand = value;
            }
        }

        private void RunProgressBar(object sender)
        {
            PropCurrentValue = 0;

            for (double i = 0; i <= PropMaxValue; i++)
            {
                PropCurrentValue++;
            }
        }
    }
I appreciate your help!
 
Last edited by a moderator:
insertcode.png
 
First question would be: have you verified if your bindings even work? If instead of initializing the current value and maximum value to 0 and 50 respectively, do you have a half filled progress bar if they were set to 50 and 100 respectively?

If yes, the next thing to check is RunProgressBar() is even being called and it if successfully running through the values.

As an aside, it's not your current issue right now, but three things caught my eye: there is nothing that will trigger a recomputation and corresponding change notification when the percentage value changes when either the current value or the max value changes; you have a potential division by zero in the brewing if the max value gets set to zero; and lastly, why the - 0 in the percentage computation?
 
Last edited:
Back
Top Bottom