Example #1
0
        private static DependencyProperty Register(DependencyPropertyHashKey key, Type propertyType, PropertyMetadata metadata, ValidateValueCallback validateValueCallback, bool isAttached, bool isReadOnly)
        {
            if (metadata == null)
            {
                metadata = new PropertyMetadata();
            }

            if (metadata.DefaultValue == null && propertyType.GetIsValueType())
            {
                metadata.DefaultValue = Activator.CreateInstance(propertyType);
            }

            if (metadata.DefaultValue != null && !propertyType.IsInstanceOfType(metadata.DefaultValue))
            {
                metadata.DefaultValue = ConvertDefaultValue(key, metadata.DefaultValue, propertyType);
            }

            DependencyProperty property = new DependencyProperty(key, propertyType, metadata, validateValueCallback, isAttached, isReadOnly);

            if (!property.IsValidValue(metadata.DefaultValue))
            {
                throw new Granular.Exception("Default value validation of dependency property \"{0}.{1}\" failed", key.Owner.Name, key.Name);
            }

            AddRegisteredProperty(key, property);

            return(property);
        }
Example #2
0
            public override bool Equals(object obj)
            {
                DependencyPropertyHashKey other = obj as DependencyPropertyHashKey;

                return(Object.ReferenceEquals(this, other) || !Object.ReferenceEquals(other, null) &&
                       Object.Equals(this.Owner, other.Owner) &&
                       Object.Equals(this.Name, other.Name));
            }
Example #3
0
        public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata metadata = null, ValidateValueCallback validateValueCallback = null)
        {
            DependencyPropertyHashKey key         = new DependencyPropertyHashKey(ownerType, name);
            DependencyPropertyKey     readOnlyKey = new DependencyPropertyKey(Register(key, propertyType, metadata, validateValueCallback, true, true));

            registeredReadOnlyPropertiesKey.Add(key, readOnlyKey);
            return(readOnlyKey);
        }
Example #4
0
        private static void VerifyNotRegistered(DependencyPropertyHashKey key, DependencyProperty dependencyProperty)
        {
            DependencyProperty registeredDependencyProperty;

            if (registeredProperties.TryGetValue(key, out registeredDependencyProperty))
            {
                throw new Granular.Exception("Can't register dependency property \"{0}\", Type \"{1}\" already has a dependency property with the same name \"{2}\"", dependencyProperty, key.Owner.Name, registeredDependencyProperty);
            }
        }
Example #5
0
 private static object ConvertDefaultValue(DependencyPropertyHashKey key, object defaultValue, Type propertyType)
 {
     try
     {
         return(Granular.Compatibility.Convert.ChangeType(defaultValue, propertyType));
     }
     catch (Exception e)
     {
         throw new Granular.Exception("Dependency property \"{0}.{1}\" default value \"{2}\" cannot be converted to \"{3}\" ({4})", key.Owner.Name, key.Name, defaultValue, propertyType, e.Message);
     }
 }
        private DependencyProperty(DependencyPropertyHashKey hashKey, Type propertyType, PropertyMetadata metadata, ValidateValueCallback validateValueCallback, bool isAttached, bool isReadOnly)
        {
            this.hashKey               = hashKey;
            this.Name                  = hashKey.Name;
            this.OwnerType             = hashKey.Owner;
            this.PropertyType          = propertyType;
            this.ValidateValueCallback = validateValueCallback;
            this.IsReadOnly            = isReadOnly;
            this.Inherits              = metadata.Inherits;

            this.ownerMetadata = metadata;
            this.isAttached    = isAttached;

            typeMetadata = new Dictionary <Type, PropertyMetadata>();
            typeMetadata.Add(OwnerType, ownerMetadata);

            typeMetadataCache = new CacheDictionary <Type, PropertyMetadata>(ResolveTypeMetadata);
        }
Example #7
0
        private DependencyProperty(DependencyPropertyHashKey hashKey, Type propertyType, PropertyMetadata metadata, ValidateValueCallback validateValueCallback, bool isAttached, bool isReadOnly)
        {
            this.hashKey               = hashKey;
            this.Name                  = hashKey.Name;
            this.OwnerType             = hashKey.Owner;
            this.PropertyType          = propertyType;
            this.ValidateValueCallback = validateValueCallback;
            this.IsReadOnly            = isReadOnly;
            this.Inherits              = metadata.Inherits;
            this.StringKey             = hashKey.StringKey;
            this.hashCode              = hashKey.GetHashCode();

            this.ownerMetadata = metadata;
            this.IsAttached    = isAttached;

            typeMetadata = new ConvertedStringDictionary <Type, PropertyMetadata>(type => type.FullName);
            typeMetadata.Add(OwnerType, ownerMetadata);

            typeMetadataCache = CacheDictionary <Type, PropertyMetadata> .CreateUsingStringKeys(ResolveTypeMetadata, type => type.FullName);

            typeContainsCache = CacheDictionary <Type, bool> .CreateUsingStringKeys(ResolveTypeContains, type => type.FullName);
        }
Example #8
0
 private static void AddRegisteredProperty(DependencyPropertyHashKey key, DependencyProperty dependencyProperty)
 {
     VerifyNotRegistered(key, dependencyProperty);
     registeredProperties.Add(key, dependencyProperty);
     typeRegisteredProperties.Add(key.Owner, dependencyProperty);
 }