예제 #1
0
        /// <summary>
        /// Registers a <see cref="AvaloniaProperty"/>.
        /// </summary>
        /// <typeparam name="TOwner">The type of the class that is registering the property.</typeparam>
        /// <typeparam name="TValue">The type of the property's value.</typeparam>
        /// <param name="name">The name of the property.</param>
        /// <param name="defaultValue">The default value of the property.</param>
        /// <param name="inherits">Whether the property inherits its value.</param>
        /// <param name="defaultBindingMode">The default binding mode for the property.</param>
        /// <param name="validate">A value validation callback.</param>
        /// <param name="coerce">A value coercion callback.</param>
        /// <param name="notifying">
        /// A method that gets called before and after the property starts being notified on an
        /// object; the bool argument will be true before and false afterwards. This callback is
        /// intended to support IsDataContextChanging.
        /// </param>
        /// <returns>A <see cref="StyledProperty{TValue}"/></returns>
        public static StyledProperty <TValue> Register <TOwner, TValue>(
            string name,
            TValue defaultValue            = default(TValue),
            bool inherits                  = false,
            BindingMode defaultBindingMode = BindingMode.OneWay,
            Func <TValue, bool> validate   = null,
            Func <IAvaloniaObject, TValue, TValue> coerce = null,
            Action <IAvaloniaObject, bool> notifying      = null)
            where TOwner : IAvaloniaObject
        {
            Contract.Requires <ArgumentNullException>(name != null);

            var metadata = new StyledPropertyMetadata <TValue>(
                defaultValue,
                defaultBindingMode: defaultBindingMode,
                coerce: coerce);

            var result = new StyledProperty <TValue>(
                name,
                typeof(TOwner),
                metadata,
                inherits,
                validate,
                notifying);

            AvaloniaPropertyRegistry.Instance.Register(typeof(TOwner), result);
            return(result);
        }
        /// <summary>
        /// Attaches the property as a non-attached property on the specified type.
        /// </summary>
        /// <typeparam name="TOwner">The owner type.</typeparam>
        /// <returns>The property.</returns>
        public StyledProperty <TValue> AddOwner <TOwner>() where TOwner : IAvaloniaObject
        {
            var result = new StyledProperty <TValue>(this, typeof(TOwner));

            AvaloniaPropertyRegistry.Instance.Register(typeof(TOwner), result);
            return(result);
        }