WPF中进度条同步实现


WPF界面的编写

滑动条的显示

//前台界面的设计    

                    
                        
                            
                            
                        
                        
                            
                            
                        
                        
                        
                        
                            
                        
                    

 与滑动条同步的画笔大小

  
      
       
      
       
    

相关属性的使用

  • 画笔大小的设置
    //画笔默认是初始值是15
    private double penWidth = 15;
            public double PenWidth {
                get {
                    return penWidth;
                }
                set {
                    Set(ref penWidth, value);
                    OnDragDelta?.Invoke();
                }
            }
  • ?. :是在C#6.0中才有的,这个也称为空值传播运算,这个运算可以确保在事件没有订阅是不引发事件的
  •  OnDragDelta?.Invoke();中的OnDragDelta,是在最开始进行封装了"public event Action OnDragDelta;"
  • 画笔最大值属性的设置
  •  private int maxPenWidth;
            public int MaxPenWidth {
                get {
                    return maxPenWidth;
                }
                set {
                    Set(ref maxPenWidth, value);
                    PenWidth = value * 0.2;
                }
            }
  • Scale属性可以通过鼠标,加与减相关命令来控制画笔大小
  • private double scale = 1.0;
            public double Scale {
                get {
                    return scale;
                }
                set {
                    Set(ref scale, value);
                }
            }
  • 相关控制的命令
  • private RelayCommand reduceVisualCommand = null;
            public RelayCommand ReduceVisualCommand {
                get {
                    return reduceVisualCommand ?? new RelayCommand(() => {
                        double targetScale = Math.Round(Scale - 0.1, 1);
                        if (targetScale < 0.4) {
                            targetScale = 0.4;
                        }
                        Scale = targetScale;
                    });
                }
            }
    
            private RelayCommand mouseWheelCommand = null;
            public RelayCommand MouseWheelCommand {
                get {
                    return mouseWheelCommand ?? new RelayCommand((delta) => {
                        double targetScale = Scale + delta * 0.001;
                        if (targetScale > 10.0) {
                            targetScale = 10.0;
                        }
                        if (targetScale < 0.4) {
                            targetScale = 0.4;
                        }
                        Scale = targetScale;
                    });
                }
            }
    
            private RelayCommand addVisualCommand = null;
            public RelayCommand AddVisualCommand {
                get {
                    return addVisualCommand ?? new RelayCommand(() => {
                        double targetScale = Math.Round(Scale + 0.1, 1);
                        if (targetScale > 10.0) {
                            targetScale = 10.0;
                        }
                        Scale = targetScale;
                    });
                }
            }

相关