Пример #1
0
        internal void AddPropertyChangeCallback(PropertyChangedCallback callback)
        {
            if (this is CustomDependencyProperty)
            {
                Console.WriteLine("this should really just be done by registering the property with metadata");
                CustomDependencyProperty cdp = (CustomDependencyProperty)this;
                if (cdp.property_changed_callback != null)
                {
                    throw new InvalidOperationException("this DP was registered with a PropertyChangedCallback already");
                }
            }

            if (change_cb != null)
            {
                throw new InvalidOperationException("this DP already has a change callback registered");
            }

            change_cb = callback;

            NativeMethods.dependency_property_set_property_changed_callback(native,
                                                                            CustomUnmanagedPropertyChangedCallbackSafe);
        }
Пример #2
0
        private static DependencyProperty RegisterAny(string name, Type propertyType, Type ownerType, PropertyMetadata metadata, bool attached, bool readOnly, bool setsParent, bool custom)
        {
            ManagedType property_type;
            ManagedType owner_type;
            UnmanagedPropertyChangeHandler handler = null;
            CustomDependencyProperty       result;
            bool   is_nullable   = false;
            object default_value = DependencyProperty.UnsetValue;
            PropertyChangedCallback property_changed_callback = null;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (name.Length == 0)
            {
                throw new ArgumentException("The 'name' argument cannot be an empty string");
            }

            if (propertyType == null)
            {
                throw new ArgumentNullException("propertyType");
            }

            if (ownerType == null)
            {
                throw new ArgumentNullException("ownerType");
            }

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                is_nullable = true;
                // Console.WriteLine ("DependencyProperty.RegisterAny (): found nullable {0}, got nullable {1}", propertyType.FullName, propertyType.GetGenericArguments () [0].FullName);
                propertyType = propertyType.GetGenericArguments() [0];
            }

            Types types = Deployment.Current.Types;

            property_type = types.Find(propertyType);
            owner_type    = types.Find(ownerType);

            if (metadata != null)
            {
                default_value             = metadata.DefaultValue ?? UnsetValue;
                property_changed_callback = metadata.property_changed_callback;
            }

            if ((default_value == DependencyProperty.UnsetValue) && propertyType.IsValueType && !is_nullable)
            {
                default_value = Activator.CreateInstance(propertyType);
            }

            if (default_value != null && default_value != UnsetValue && !propertyType.IsAssignableFrom(default_value.GetType()))
            {
                throw new ArgumentException(string.Format("DefaultValue is of type {0} which is not compatible with type {1}", default_value.GetType(), propertyType));
            }

            if (property_changed_callback != null)
            {
                handler = UnmanagedPropertyChangedCallbackSafe;
            }

            Value v;

            if (default_value == DependencyProperty.UnsetValue)
            {
                v = new Value {
                    Kind = types.TypeToKind(propertyType), IsNull = true
                };
                default_value = null;
            }
            else
            {
                v = Value.FromObject(default_value, true);
            }

            IntPtr handle;

            using (v) {
                var val = v;
                if (custom)
                {
                    handle = NativeMethods.dependency_property_register_custom_property(name, property_type.native_handle, owner_type.native_handle, ref val, attached, readOnly, handler);
                }
                else
                {
                    handle = NativeMethods.dependency_property_register_core_property(name, property_type.native_handle, owner_type.native_handle, ref val, attached, readOnly, handler);
                }
            }

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            if (is_nullable)
            {
                NativeMethods.dependency_property_set_is_nullable(handle, true);
            }

            result          = new CustomDependencyProperty(handle, name, property_type, owner_type, default_value);
            result.attached = attached;
            result.PropertyChangedHandler    = handler;
            result.property_changed_callback = property_changed_callback;

            return(result);
        }
Пример #3
0
        private static DependencyProperty RegisterAny(string name, Type propertyType, Type ownerType, PropertyMetadata metadata, bool attached, bool readOnly, bool setsParent, bool custom)
        {
            ManagedType property_type;
            ManagedType owner_type;
            UnmanagedPropertyChangeHandler handler = null;
            CustomDependencyProperty result;
            bool is_nullable = false;
            object default_value = DependencyProperty.UnsetValue;
            PropertyChangedCallback property_changed_callback = null;

            if (name == null)
                throw new ArgumentNullException ("name");

            if (name.Length == 0)
                throw new ArgumentException("The 'name' argument cannot be an empty string");

            if (propertyType == null)
                throw new ArgumentNullException ("propertyType");

            if (ownerType == null)
                throw new ArgumentNullException ("ownerType");

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition () == typeof (Nullable<>)) {
                is_nullable = true;
                // Console.WriteLine ("DependencyProperty.RegisterAny (): found nullable {0}, got nullable {1}", propertyType.FullName, propertyType.GetGenericArguments () [0].FullName);
                propertyType = propertyType.GetGenericArguments () [0];
            }

            Types types = Deployment.Current.Types;

            property_type = types.Find (propertyType);
            owner_type = types.Find (ownerType);

            if (metadata != null) {
                default_value = metadata.DefaultValue ?? UnsetValue;
                property_changed_callback = metadata.property_changed_callback;
            }

            if ((default_value == DependencyProperty.UnsetValue) && propertyType.IsValueType && !is_nullable)
                default_value = Activator.CreateInstance (propertyType);

            if (default_value != null && default_value != UnsetValue && !propertyType.IsAssignableFrom (default_value.GetType ()))
                throw new ArgumentException (string.Format ("DefaultValue is of type {0} which is not compatible with type {1}", default_value.GetType (), propertyType));

            if (property_changed_callback != null)
                handler = UnmanagedPropertyChangedCallbackSafe;

            Value v;
            if (default_value == DependencyProperty.UnsetValue) {
                v = new Value { k = types.TypeToKind (propertyType), IsNull = true };
                default_value = null;
            } else {
                v = Value.FromObject (default_value, true);
            }

            IntPtr handle;
            using (v) {
                var val = v;
                if (custom)
                    handle = NativeMethods.dependency_property_register_custom_property (name, property_type.native_handle, owner_type.native_handle, ref val, attached, readOnly, handler);
                else
                    handle = NativeMethods.dependency_property_register_core_property (name, property_type.native_handle, owner_type.native_handle, ref val, attached, readOnly, handler);
            }

            if (handle == IntPtr.Zero)
                return null;

            if (is_nullable)
                NativeMethods.dependency_property_set_is_nullable (handle, true);

            result = new CustomDependencyProperty (handle, name, property_type, owner_type, default_value);
            result.attached = attached;
            result.PropertyChangedHandler = handler;
            result.property_changed_callback = property_changed_callback;

            return result;
        }