ProgressBar没有't work properly

Kerenshor.

新成员
加入
2016年1月23日
消息
4
编程经验
1-3
我正在使用c#wpf mvvm。

ProgressBar在一段时间之后彩色红色,但它没有显示任何移动,并且百分比没有递增,并且保持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++;
            }
        }
    }
我感谢您的帮助!
 
最后编辑了主持人:

约翰

C#论坛主持人
工作人员
加入
2011年4月23日
消息
1,159
地点
挪威
编程经验
10+
InsertCode.png.
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,893
地点
切萨皮克,va.
编程经验
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:
最佳 底部