Exemple #1
0
 public static void ApplyDefaultValuePropertyNames(XDependencyObject instance, string names)
 {
     if (string.IsNullOrEmpty(names) == false && instance != null)
     {
         foreach (string name in names.Split(','))
         {
             bool match = false;
             foreach (XDependencyProperty p in instance.InnerValues.Keys)
             {
                 if (p.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                 {
                     match = true;
                     break;
                 }
             }//foreach
             if (match == false)
             {
                 XDependencyProperty p = XDependencyProperty.GetProperty(instance.GetType(), name);
                 if (p != null)
                 {
                     instance.InnerValues[p] = p.DefaultValue;
                 }
             }
         }//foreach
     }
 }
Exemple #2
0
        public void Log(
            XDependencyObject instance,
            XDependencyProperty property,
            object oldValue,
            object newValue)
        {
            if (this.Enabled == false)
            {
                return;
            }
            foreach (LogItem item in this._Items)
            {
                if (item.Instance == instance && item.Property == property)
                {
                    item.NewValue = newValue;
                    return;
                }
            }
            LogItem newItem = new LogItem();

            newItem.Instance = instance;
            newItem.Property = property;
            newItem.OldValue = oldValue;
            newItem.NewValue = newValue;
            this._Items.Add(newItem);
        }
 public XDependencyPropertyDescriptor(XDependencyProperty property, Attribute[] attrs)
     : base(property.Name, attrs)
 {
     if (property == null)
     {
         throw new ArgumentNullException("property");
     }
     _Property = property;
 }
Exemple #4
0
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);

            XDependencyProperty[] ps = XDependencyProperty.GetProperties(this.GetType(), false);
            foreach (XDependencyProperty p in ps)
            {
                properties.Add(new XDependencyPropertyDescriptor(p, attributes));
            }
            return(properties);
        }
 /// <summary>
 /// 初始化对象
 /// </summary>
 /// <param name="instance">对象实例</param>
 /// <param name="property">属性</param>
 /// <param name="Value">数值</param>
 /// <param name="newValue">新数值</param>
 internal XDependencyObjectEventArgs(
     XDependencyObject instance,
     XDependencyProperty property,
     object Value,
     object newValue)
 {
     _instance = instance;
     _Property = property;
     _Value    = Value;
     _NewValue = newValue;
 }
        /// <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,
            object defaultValue)
        {
            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");
            }
            if (defaultValue != null)
            {
                if (propertyType.IsInstanceOfType(defaultValue) == false)
                {
                    throw new ArgumentException("bad defaultValue:" + defaultValue);
                }
            }
            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 = defaultValue;
            table[name]           = property;
            return(property);
        }
        /// <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);
        }
Exemple #8
0
 /// <summary>
 /// 获得对象数据
 /// </summary>
 /// <param name="property">属性对象</param>
 /// <returns>获得的数据</returns>
 public virtual object GetValue(XDependencyProperty property)
 {
     if (property == null)
     {
         throw new ArgumentNullException("property");
     }
     if (property.OwnerType.IsInstanceOfType(this) == false)
     {
         throw new ArgumentException("need " + property.OwnerType.FullName);
     }
     _InnerValues.CheckState(this.GetType());
     if (_InnerValues.ContainsKey(property))
     {
         return(_InnerValues[property]);
     }
     else
     {
         return(property.DefaultValue);
     }
 }
Exemple #9
0
 /// <summary>
 /// 设置对象数据
 /// </summary>
 /// <param name="property">属性</param>
 /// <param name="Value">属性值</param>
 public virtual void SetValue(XDependencyProperty property, object Value)
 {
     if (_ValueLocked)
     {
         throw new InvalidOperationException("Locked");
     }
     if (property == null)
     {
         throw new ArgumentNullException("property");
     }
     if (property.OwnerType.IsInstanceOfType(this) == false)
     {
         throw new ArgumentException("need " + property.OwnerType.FullName);
     }
     _InnerValues.CheckState(this.GetType());
     if (ValueChanging != null || this is IXDependencyPropertyLoggable)
     {
         object oldValue = null;
         if (_InnerValues.ContainsKey(property))
         {
             oldValue = _InnerValues[property];
         }
         else
         {
             oldValue = property.DefaultValue;
         }
         if (ValueChanging != null)
         {
             XDependencyObjectEventArgs args = new XDependencyObjectEventArgs(
                 this,
                 property,
                 oldValue,
                 Value);
             ValueChanging(this, args);
             if (args.Cancel)
             {
                 return;
             }
             Value = args.NewValue;
         }
         if (this is IXDependencyPropertyLoggable)
         {
             IXDependencyPropertyLogger logger =
                 ((IXDependencyPropertyLoggable)this).PropertyLogger;
             if (logger != null && logger.Enabled)
             {
                 logger.Log(this, property, oldValue, Value);
             }
         }
         if (this._DisableDefaultValue == false &&
             property.EqualsDefaultValue(Value))
         {
             if (_InnerValues.ContainsKey(property))
             {
                 _InnerValues.Remove(property);
             }
         }
         else
         {
             _InnerValues[property] = Value;
         }
         if (ValueChanged != null)
         {
             XDependencyObjectEventArgs args2 = new XDependencyObjectEventArgs(
                 this,
                 property,
                 Value,
                 Value);
             ValueChanged(this, args2);
         }
     }
     else
     {
         if (property.EqualsDefaultValue(Value))
         {
             if (_InnerValues.ContainsKey(property))
             {
                 _InnerValues.Remove(property);
             }
         }
         else
         {
             _InnerValues[property] = Value;
         }
         if (ValueChanged != null)
         {
             XDependencyObjectEventArgs args2 = new XDependencyObjectEventArgs(
                 this,
                 property,
                 Value,
                 Value);
             ValueChanged(this, args2);
         }
     }
     this._ValueModified = true;
 }