ProgressBar没有't work properly

克伦索尔

新成员
已加入
2016年1月23日
留言内容
1
编程经验
1-3
我正在使用C#WPF MVVM。

进度栏过一会儿会变成红色,但是它没有显示任何移动,并且百分比不会增加,而是保持在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++;
            }
        }
    }
我感谢您的帮助!
 
由主持人最后编辑:

约翰·H

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
1,049
地点
挪威
编程经验
10+
insertcode.png
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,532
地点
弗吉尼亚州切萨皮克
编程经验
10+
第一个问题是:您是否已验证绑定是否有效?如果不是将当前值和最大值分别初始化为0和50,而是分别将其分别设置为50和100,则是否有一个半填充进度条?

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:
最佳 底部