Пример #1
0
        /// <summary>
        ///     Прямое связывание
        /// </summary>
        /// <param name="val">значение</param>
        /// <param name="type">тип</param>
        /// <param name="direction">направление</param>
        /// <returns>Признак успешного связывания</returns>
        public override bool DirectBind(ref object val, Type type, BindingDirection direction)
        {
            var changed = false;
            if (direction == BindingDirection.FromSource)
            {
                if (val == null)
                    Value = "";
                else if (type == typeof(decimal))
                    ValueDecimal = (decimal) val;
                else if (type == typeof(int))
                    ValueInt = (int) val;
                else
                    Value = val.ToString();
            }
            else
            {
                object newval = null;
                if (type == typeof(string))
                {
                    newval = Value;
                }
                else if (type == typeof(int) || type == typeof(int?))
                {
                    if (Value.Length > 0)
                        newval = ValueInt;
                }
                else if (type == typeof(short) || type == typeof(short?))
                {
                    if (Value.Length > 0)
                        newval = short.Parse(Value, NumberStyles.Any);
                }
                else if (type == typeof(decimal))
                {
                    newval = ValueDecimal;
                }
                else if (type == typeof(decimal?))
                {
                    if (Value.Length > 0)
                        newval = ValueDecimal;
                }

                if (newval == null && val != null)
                    changed = true;
                else if (newval != null)
                    changed = !newval.Equals(val);
                val = newval;
            }

            return changed;
        }
        /// <summary>
        ///     Связывание
        /// </summary>
        /// <param name="val">значение</param>
        /// <param name="type">тип</param>
        /// <param name="direction">направление</param>
        /// <returns>Признак успешного связывания</returns>
        public override bool DirectBind(ref object val, Type type, BindingDirection direction)
        {
            var changed = false;

            if (direction == BindingDirection.FromSource)
            {
                if (val == null)
                {
                    Value = "";
                }
                else if (type == typeof(DateTime))
                {
                    Value = ((DateTime)val).ToString(_dateFormat);
                }
                else if (type == typeof(DateTime?))
                {
                    Value = ((DateTime?)val).Value.ToString(_dateFormat);
                }
            }
            else
            {
                object newval = null;
                if (Value.Length > 0)
                {
                    newval = DateTime.ParseExact(Value, _dateFormat, CultureInfo.GetCultureInfo("ru-RU"));
                }

                if (newval == null && val != null)
                {
                    changed = true;
                }
                else if (newval != null)
                {
                    changed = !newval.Equals(val);
                }

                val = newval;
            }

            return(changed);
        }