private void InitValidate() { if (QuestionList.Any()) { #region 验证条件 var validates = QuestionList.Where(x => x.Question.q_is_validate && !string.IsNullOrWhiteSpace(x.Question.q_validate)); //找到有验证条件的 foreach (UCQuestion validate in validates) { tmo_questionnaire que = validate.Question; Dictionary <string, string> dic = TmoShare.GetValueFromJson <Dictionary <string, string> >(que.q_validate); if (dic == null || !dic.Any()) { continue; } foreach (KeyValuePair <string, string> keyValuePair in dic) { string type = keyValuePair.Key; string conditionStr = keyValuePair.Value; if (type == "calc") //自动计算 { List <string> qidList = new List <string>(); var macth = Regex.Match(conditionStr, @"\{.+?\}"); while (macth.Success) { string qid = macth.Value; if (!qidList.Contains(qid)) { qidList.Add(qid); } macth = macth.NextMatch(); } List <UCQuestion> tquelist = new List <UCQuestion>(); qidList.ForEach(x => { string qid = x.TrimStart('{').TrimEnd('}'); try { var tucque = QuestionList.First(y => y.Question.q_id == qid); tquelist.Add(tucque); } catch { } }); tquelist.ForEach(x => { x.Leave += ((sender, args) => { string str = conditionStr; foreach (UCQuestion y in tquelist) { if (!y.ValidateValue()) { return; //值类型验证失败 停止计算 } string tovale = y.GetValue().ToString(); if (string.IsNullOrWhiteSpace(tovale)) { return; } string replacekey = "{" + y.Question.q_id + "}"; str = str.Replace(replacekey, tovale); } object obj = TmoShare.CalcString(str); validate.SetValue(obj); }); }); } else if (type.StartsWith("enable") || type.StartsWith("disable")) //禁用选项 { string tqid = conditionStr; try { var tucque = QuestionList.First(y => y.Question.q_id == tqid); tucque.ValueChanged += (sender) => { object value = tucque.GetValue(); if (value is DateTime) { DateTime dt = (DateTime)value; if (dt != DateTime.MinValue && dt != new DateTime(9999, 12, 31)) { validate.Enabled = true; } else { validate.Enabled = false; validate.SetValue(null); } } if (value is bool) { if (type == "disable" || type == "enable") { bool enable = (bool)value; if (type == "disable") { enable = !enable; } if (!enable) { validate.SetValue(null); } validate.Enabled = enable; } } if (value is float) { if (type.Contains("|")) { float f = (float)value; var list = StringPlus.GetStrArray(StringPlus.GetStrArray(type, "-")[1], "|"); var flist = new List <float>(); list.ForEach(x => flist.Add(Convert.ToSingle(x))); bool enable = flist.Contains(f); if (type.StartsWith("disable")) { enable = !enable; } if (!enable) { validate.SetValue(null); } validate.Enabled = enable; } } }; tucque.OnValueChanged(); } catch { } } else if (type == "required") //必填验证 { validate.IsRequired = true; } } } #endregion #region 男女验证 var genders = QuestionList.Where(x => x.Question.q_gender != 0); //找到有性别限制的题目 foreach (UCQuestion gender in genders) { if (_user.gender != gender.Question.q_gender) { gender.Enabled = false; } } #endregion } }