2011-10-06 96 views
0

我有下面的类:C#依赖房地产/物业胁迫

public class Numbers :INotifyPropertyChanged 
{ 
    private double _Max; 
    public double Max 
    { 
     get 
     { 
      return this._Max; 
     } 
     set 
     { 
      if (value >= _Min) 
      { 
       this._Max = value; 
       this.NotifyPropertyChanged("Max"); 
      } 
     } 
    } 

    private double _Min; 
    public double Min 
    { 
     get 
     { 
      return this._Min; 
     } 
     set 
     { 
      if (value <= Max) 
      { 
       this._Min = value; 
       this.NotifyPropertyChanged("Min"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (this.PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    }   
} 

问题:我不想让用户输入最大值小于最小值等。但是,当最小/最大值的默认值为零时,其他班级尝试设置最小值/最大值时,上面的代码第一次不工作。 由于默认情况下最小值和最大值将为零,如果min值设置为> 0,这在逻辑上是正确的,但不允许约束。 我想我需要解决这个使用依赖属性或强制。任何人都可以指导做到这一点?

+1

为什么最小值和最大值为0默认?为什么不Double.MinDouble和Double.MaxDouble? –

回答

1

所以它成为你可以通过一个可空支持它这样的:

public class Numbers : INotifyPropertyChanged 
{ 
    private double? _Max; 
    public double Max 
    { 
     get 
     { 
      return _Max ?? 0; 
     } 
     set 
     { 
      if (value >= _Min || !_Max.HasValue) 
      { 
       this._Max = value; 
       this.NotifyPropertyChanged("Max"); 
      } 
     } 
    } 

    private double? _Min; 
    public double Min 
    { 
     get 
     { 
      return this._Min ?? 0; 
     } 
     set 
     { 
      if (value <= Max || !_Min.HasValue) 
      { 
       this._Min = value; 
       this.NotifyPropertyChanged("Min"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (this.PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 
+0

谢谢。这是我的预期。 – Suresh

+0

我已经测试过,但不能按预期工作。它最终允许用户输入小于最小值的最大值。 – Suresh

2

将_Max初始化为Double.MaxValue,_Min为Double.MinValue。

0

我不知道如果我理解正确的话,但如果该值越来越为第一组,你可以有一个private bool指示时间,从而压倒支票。

了我的头:

private bool _FirstTimeSet = false; 
    private double _Max; 
    public double Max 
    { 
     get 
     { 
      return this._Max; 
     } 
     set 
     { 
      if (value >= _Min || _FirstTimeSet == false) 
      { 
       this._FirstTimeSet = true; 
       this._Max = value; 
       this.NotifyPropertyChanged("Max"); 
      } 
     } 
    }