开发者

C# dependent property / property coercion

I have the following class:

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));
    }        
}

Problem: I dont want to allow user t开发者_如何学JAVAo enter max value less than min value and so on. But above code is not working for the first time when other class try to set min / max value when min / max value has default value of zero. Since by default min and max value will be zero, if min value is set > 0 which is logically correct but the constraint is not allowed to do that. I think I need to solve this using dependent property or coercion. Could anyone guide to do that?


Initialize _Max to Double.MaxValue, _Min to Double.MinValue.


You could back it by a Nullable so it becomes this:

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));
    }
}


I don't know if i understand you correctly, but you could have a private bool indicating if the value is getting set for the first time and thus overriding the check.

out of my head:

    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");
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜