コード例 #1
0
        //public XDependencyProperty()
        //{
        //}

        private XDependencyProperty(
            Type ownerType,
            string name,
            Type propertyType)
        {
            _OwnerType    = ownerType;
            _Name         = name;
            _PropertyType = propertyType;
            _DefaultValue = ValueTypeHelper.GetDefaultValue(_PropertyType);
        }
コード例 #2
0
        /// <summary>
        /// 注册属性
        /// </summary>
        /// <param name="name">属性名</param>
        /// <param name="propertyType">属性数据类型</param>
        /// <param name="ownerType">属性所属对象类型</param>
        /// <returns>属性注册对象</returns>
        public static XDependencyProperty Register(
            string name,
            Type propertyType,
            Type ownerType)
        {
            if (name == null || name.Trim().Length == 0)
            {
                throw new ArgumentNullException("name");
            }
            name = name.Trim();
            if (propertyType == null)
            {
                throw new ArgumentNullException("propertyType");
            }
            if (ownerType == null)
            {
                throw new ArgumentNullException("ownerType");
            }
            Dictionary <string, XDependencyProperty> table = null;

            if (_PropertiyTable.ContainsKey(ownerType))
            {
                table = _PropertiyTable[ownerType];
            }
            else
            {
                table = new Dictionary <string, XDependencyProperty>();
                _PropertiyTable[ownerType] = table;
            }
            if (table.ContainsKey(name))
            {
                throw new ArgumentException("Multi " + name);
            }
            XDependencyProperty property = new XDependencyProperty(
                ownerType,
                name,
                propertyType);

            property.DefaultValue = ValueTypeHelper.GetDefaultValue(propertyType);
            table[name]           = property;
            return(property);
        }
コード例 #3
0
        /// <summary>
        /// 进行数据验证
        /// </summary>
        /// <returns>验证是否通过</returns>
        public bool Validate()
        {
            strMessage = null;
            string cm = this.CustomMessage;

            if (cm == null || cm.Trim().Length == 0)
            {
                cm = null;
            }
            bool isNullValue = false;

            if (objValue == null || DBNull.Value.Equals(objValue))
            {
                isNullValue = true;
            }
            else
            {
                string txt = Convert.ToString(objValue);
                if (string.IsNullOrEmpty(txt))
                {
                    isNullValue = true;
                }
            }
            if (this.Required)
            {
                if (isNullValue)
                {
                    strMessage = cm != null ? cm : ValueValidateStyleStrings.ForbidEmpty;
                    return(false);
                }
            }
            else
            {
                if (isNullValue)
                {
                    // 数据为空而且允许为空,则不进行后续判断
                    return(true);
                }
            }
            if (intValueType == ValueTypeStyle.Text)
            {
                string txt = Convert.ToString(objValue);
                if (this.CheckMaxValue && intMaxLength > 0)
                {
                    if (txt != null && txt.Length > this.MaxLength)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.MoreThanMaxLength_Length, intMaxLength);
                        return(false);
                    }
                }
                if (this.CheckMinValue && this.MinLength > 0)
                {
                    if (txt != null && txt.Length < intMinLength)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.LessThanMinLength_Length, intMinLength);
                        return(false);
                    }
                }
                if (strRange != null && strRange.Length > 0)
                {
                    bool     find  = true;
                    string[] items = strRange.Split(new char[] { ',' });
                    foreach (string item in items)
                    {
                        find = false;
                        if (string.Compare(txt, item.Trim(), true) == 0)
                        {
                            find = true;
                            break;
                        }
                    }
                    if (find == false)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.ExcludeRange_Range, strRange);
                        return(false);
                    }
                }
            }
            else if (intValueType == ValueTypeStyle.Numeric ||
                     intValueType == ValueTypeStyle.Integer)
            {
                double v = double.NaN;
                if (objValue is Int32 || objValue is float || objValue is double)
                {
                    v = (double)objValue;
                }
                else
                {
                    bool result = false;
                    if (intValueType == ValueTypeStyle.Numeric)
                    {
                        result = double.TryParse(Convert.ToString(objValue), out v);
                        if (result == false)
                        {
                            strMessage = cm != null ? cm : ValueValidateStyleStrings.MustNumeric;
                            return(false);
                        }
                    }
                    else
                    {
                        int v2 = int.MinValue;
                        result = ValueTypeHelper.TryParseInt32(Convert.ToString(objValue), out v2);
                        if (result)
                        {
                            v = v2;
                        }
                        else
                        {
                            strMessage = cm != null ? cm : ValueValidateStyleStrings.MustInteger;
                            return(false);
                        }
                    }
                }
                if (this.CheckMaxValue && double.IsNaN(dblMaxValue) == false)
                {
                    if (v > dblMaxValue)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.MoreThanMaxValue_Value, dblMaxValue);
                        return(false);
                    }
                }
                if (this.CheckMinValue && double.IsNaN(dblMinValue) == false)
                {
                    if (v < dblMinValue)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.LessThanMinValue_Value, dblMinValue);
                        return(false);
                    }
                }
                if (strRange != null && strRange.Length > 0)
                {
                    bool     find  = true;
                    string[] items = strRange.Split(new char[] { ',' });
                    foreach (string item in items)
                    {
                        find = false;
                        int index = item.IndexOf('-');
                        if (index > 0)
                        {
                            double min = 0;
                            double max = 0;
                            if (double.TryParse(item.Substring(0, index), out min) &&
                                double.TryParse(item.Substring(index + 1), out max))
                            {
                                if (v >= min && v <= max)
                                {
                                    find = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            double v2 = double.NaN;
                            if (double.TryParse(item, out v2))
                            {
                                if (v2 == v)
                                {
                                    find = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (find == false)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.ExcludeRange_Range, strRange);
                        return(false);
                    }
                }
            }
            else if (intValueType == ValueTypeStyle.Date)
            {
                DateTime dtm    = DateTime.MinValue;
                bool     result = DateTime.TryParse(Convert.ToString(objValue), out dtm);
                if (result == false)
                {
                    strMessage = cm != null ? cm : ValueValidateStyleStrings.MustDateType;
                    return(false);
                }
                dtm = dtm.Date;
                if (this.CheckMaxValue)
                {
                    DateTime max = dtmDateTimeMaxValue.Date;
                    if (dtm > max)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.MoreThanMaxValue_Value, max.ToString("yyyy-MM-dd"));
                        return(false);
                    }
                }
                if (this.CheckMinValue)
                {
                    DateTime min = dtmDateTimeMinValue.Date;
                    if (dtm < min)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.LessThanMinValue_Value, min.ToString("yyyy-MM-dd"));
                        return(false);
                    }
                }
            }
            else if (intValueType == ValueTypeStyle.Time)
            {
                TimeSpan dtm    = TimeSpan.Zero;
                bool     result = TimeSpan.TryParse(Convert.ToString(objValue), out dtm);
                if (result == false)
                {
                    strMessage = cm != null ? cm : ValueValidateStyleStrings.MustTimeType;
                    return(false);
                }
                if (this.CheckMaxValue)
                {
                    TimeSpan max = dtmDateTimeMaxValue.TimeOfDay;
                    if (dtm > max)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.MoreThanMaxValue_Value, max);
                        return(false);
                    }
                }
                if (this.CheckMinValue)
                {
                    TimeSpan min = dtmDateTimeMinValue.TimeOfDay;
                    if (dtm < min)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.LessThanMinValue_Value, min);
                        return(false);
                    }
                }
            }
            else if (intValueType == ValueTypeStyle.DateTime)
            {
                DateTime dtm    = DateTime.MinValue;
                bool     result = DateTime.TryParse(Convert.ToString(objValue), out dtm);
                if (result == false)
                {
                    strMessage = cm != null ? cm : ValueValidateStyleStrings.MustDateTimeType;
                    return(false);
                }
                if (this.CheckMaxValue)
                {
                    DateTime max = dtmDateTimeMaxValue;
                    if (dtm > max)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.MoreThanMaxValue_Value, max.ToString("yyyy-MM-dd HH:mm:ss"));
                        return(false);
                    }
                }
                if (this.CheckMinValue)
                {
                    DateTime min = dtmDateTimeMinValue;
                    if (dtm < min)
                    {
                        strMessage = cm != null ? cm : string.Format(ValueValidateStyleStrings.LessThanMinValue_Value, min.ToString("yyyy-MM-dd HH:mm:ss"));
                        return(false);
                    }
                }
            }
            return(true);
        }