コード例 #1
0
        /// <summary>
        /// Registers an attached <see cref="AvaloniaProperty"/>.
        /// </summary>
        /// <typeparam name="TOwner">The type of the class that is registering the property.</typeparam>
        /// <typeparam name="THost">The type of the class that the property is to be registered on.</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 validation function.</param>
        /// <returns>A <see cref="AvaloniaProperty{TValue}"/></returns>
        public static AttachedProperty <TValue> RegisterAttached <TOwner, THost, TValue>(
            string name,
            TValue defaultValue                   = default(TValue),
            bool inherits                         = false,
            BindingMode defaultBindingMode        = BindingMode.OneWay,
            Func <THost, TValue, TValue> validate = null)
            where THost : IAvaloniaObject
        {
            Contract.Requires <ArgumentNullException>(name != null);

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

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

            AvaloniaPropertyRegistry.Instance.Register(typeof(THost), result);
            return(result);
        }
コード例 #2
0
ファイル: AvaloniaProperty.cs プロジェクト: nirzaf/Avalonia
        /// <summary>
        /// Registers an attached <see cref="AvaloniaProperty"/>.
        /// </summary>
        /// <typeparam name="THost">The type of the class that the property is to be registered on.</typeparam>
        /// <typeparam name="TValue">The type of the property's value.</typeparam>
        /// <param name="name">The name of the property.</param>
        /// <param name="ownerType">The type of the class that is registering 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>
        /// <returns>A <see cref="AvaloniaProperty{TValue}"/></returns>
        public static AttachedProperty <TValue> RegisterAttached <THost, TValue>(
            string name,
            Type ownerType,
            TValue defaultValue            = default(TValue),
            bool inherits                  = false,
            BindingMode defaultBindingMode = BindingMode.OneWay,
            Func <TValue, bool> validate   = null,
            Func <IAvaloniaObject, TValue, TValue> coerce = null)
            where THost : IAvaloniaObject
        {
            Contract.Requires <ArgumentNullException>(name != null);

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

            var result   = new AttachedProperty <TValue>(name, ownerType, metadata, inherits, validate);
            var registry = AvaloniaPropertyRegistry.Instance;

            registry.Register(ownerType, result);
            registry.RegisterAttached(typeof(THost), result);
            return(result);
        }