How can I validate multiple properties when any of them changes?

后端 未结 4 1513

I have two date fields: StartDate and EndDate. StartDate must be earlier than EndDate.

If the user changes the StartDate to something greater than the EndDate, a red

4条回答
  •  甜味超标
    2020-12-08 15:52

    Use this trick, it prevents they call OnPropertyChanged each other :

    private bool RPCfromStartDate = false;
    private bool RPCfromEndDate = false;
    
    public string this[string columnName]
    {
        get 
        {
                    string result = null;
                    switch (columnName)
                    {
                        case "StartDate":
                            if (StartDate.Date >= EndDate.Date)
                            {
                                result = "Start Date cannot be later than End Date";
                            }
                            if (!RPCfromEndDate)
                            {
                                RPCfromStartDate = true;                            
                                OnPropertyChanged("EndDate");
                                RPCfromStartDate = false;
                            } 
                        case "EndDate":
                            if (StartDate.Date >= EndDate.Date)
                            {
                                result = "End Date cannot be earlier than Start Date";
                            }
                            if (!RPCfromStartDate)
                            {
                                RPCfromEndDate = true;                            
                                OnPropertyChanged("StartDate");
                                RPCfromEndDate = false;
                            } 
                            break;
                    }
    ...
    

提交回复
热议问题