Beispiel #1
0
        /// <summary>
        /// Registers a <see cref="PerspexProperty"/>.
        /// </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 validation function.</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="PerspexProperty{TValue}"/></returns>
        public static PerspexProperty <TValue> Register <TOwner, TValue>(
            string name,
            TValue defaultValue                    = default(TValue),
            bool inherits                          = false,
            BindingMode defaultBindingMode         = BindingMode.OneWay,
            Func <TOwner, TValue, TValue> validate = null,
            Action <PerspexObject, bool> notifying = null)
            where TOwner : PerspexObject
        {
            Contract.Requires <ArgumentNullException>(name != null);

            PerspexProperty <TValue> result = new PerspexProperty <TValue>(
                name,
                typeof(TOwner),
                defaultValue,
                inherits,
                defaultBindingMode,
                Cast(validate),
                notifying,
                false);

            PerspexObject.Register(typeof(TOwner), result);

            return(result);
        }
Beispiel #2
0
 /// <summary>
 /// Initialites a two-way bind between <see cref="PerspexProperty"/>s.
 /// </summary>
 /// <param name="property">The property on this object.</param>
 /// <param name="source">The source object.</param>
 /// <param name="sourceProperty">The property on the source object.</param>
 /// <returns>
 /// A disposable which can be used to terminate the binding.
 /// </returns>
 /// <remarks>
 /// The binding is first carried out from <paramref name="source"/> to this. Two-way
 /// bindings are always at the LocalValue priority.
 /// </remarks>
 public void BindTwoWay(
     PerspexProperty property,
     PerspexObject source,
     PerspexProperty sourceProperty)
 {
     source.GetObservable(sourceProperty).Subscribe(x => this.SetValue(property, x));
     this.GetObservable(property).Subscribe(x => source.SetValue(sourceProperty, x));
 }
Beispiel #3
0
 /// <summary>
 /// Initialites a two-way bind between <see cref="PerspexProperty"/>s.
 /// </summary>
 /// <param name="property">The property on this object.</param>
 /// <param name="source">The source object.</param>
 /// <param name="sourceProperty">The property on the source object.</param>
 /// <param name="priority">The priority of the binding.</param>
 /// <returns>
 /// A disposable which can be used to terminate the binding.
 /// </returns>
 /// <remarks>
 /// The binding is first carried out from <paramref name="source"/> to this.
 /// </remarks>
 public IDisposable BindTwoWay(
     PerspexProperty property,
     PerspexObject source,
     PerspexProperty sourceProperty,
     BindingPriority priority = BindingPriority.LocalValue)
 {
     return(new CompositeDisposable(
                Bind(property, source.GetObservable(sourceProperty)),
                source.Bind(sourceProperty, GetObservable(property))));
 }
Beispiel #4
0
        /// <summary>
        /// Registers the direct property on another type.
        /// </summary>
        /// <typeparam name="TOwner">The type of the additional owner.</typeparam>
        /// <returns>The property.</returns>
        public PerspexProperty <TValue> AddOwner <TOwner>(
            Func <TOwner, TValue> getter,
            Action <TOwner, TValue> setter = null)
            where TOwner : PerspexObject
        {
            var result = new PerspexProperty <TValue>(this, CastReturn(getter), CastParam1(setter));

            PerspexObject.Register(typeof(TOwner), result);
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Registers the property on another type.
        /// </summary>
        /// <typeparam name="TOwner">The type of the additional owner.</typeparam>
        /// <returns>The property.</returns>
        public PerspexProperty <TValue> AddOwner <TOwner>() where TOwner : PerspexObject
        {
            if (IsDirect)
            {
                throw new InvalidOperationException(
                          "You must provide a new getter and setter when calling AddOwner on a direct PerspexProperty.");
            }

            PerspexObject.Register(typeof(TOwner), this);
            return(this);
        }
Beispiel #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PriorityValue"/> class.
 /// </summary>
 /// <param name="owner">The owner of the object.</param>
 /// <param name="name">The name of the property.</param>
 /// <param name="valueType">The value type.</param>
 /// <param name="validate">An optional validation function.</param>
 public PriorityValue(
     PerspexObject owner,
     string name,
     Type valueType,
     Func <object, object> validate = null)
 {
     _owner        = owner;
     _name         = name;
     _valueType    = valueType;
     _value        = PerspexProperty.UnsetValue;
     ValuePriority = int.MaxValue;
     _validate     = validate;
 }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PerspexPropertyChangedEventArgs"/> class.
 /// </summary>
 /// <param name="sender">The object that the property changed on.</param>
 /// <param name="property">The property that changed.</param>
 /// <param name="oldValue">The old value of the property.</param>
 /// <param name="newValue">The new value of the property.</param>
 /// <param name="priority">The priority of the binding that produced the value.</param>
 public PerspexPropertyChangedEventArgs(
     PerspexObject sender,
     PerspexProperty property,
     object oldValue,
     object newValue,
     BindingPriority priority)
 {
     Sender   = sender;
     Property = property;
     OldValue = oldValue;
     NewValue = newValue;
     Priority = priority;
 }
Beispiel #8
0
        /// <summary>
        /// Initiates a two-way binding between <see cref="PerspexProperty"/>s.
        /// </summary>
        /// <param name="property">The property on this object.</param>
        /// <param name="source">The source object.</param>
        /// <param name="sourceProperty">The property on the source object.</param>
        /// <param name="priority">The priority of the binding.</param>
        /// <returns>
        /// A disposable which can be used to terminate the binding.
        /// </returns>
        /// <remarks>
        /// The binding is first carried out from <paramref name="source"/> to this.
        /// </remarks>
        public IDisposable BindTwoWay(
            PerspexProperty property,
            PerspexObject source,
            PerspexProperty sourceProperty,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            VerifyAccess();
            _propertyLog.Verbose(
                "Bound two way {Property} to {Binding} with priority {Priority}",
                property,
                source,
                priority);

            return(new CompositeDisposable(
                       Bind(property, source.GetObservable(sourceProperty)),
                       source.Bind(sourceProperty, GetObservable(property))));
        }
Beispiel #9
0
        /// <summary>
        /// Registers a direct <see cref="PerspexProperty"/>.
        /// </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="getter">Gets the current value of the property.</param>
        /// <param name="setter">Sets the value of the property.</param>
        /// <returns>A <see cref="PerspexProperty{TValue}"/></returns>
        public static PerspexProperty <TValue> RegisterDirect <TOwner, TValue>(
            string name,
            Func <TOwner, TValue> getter,
            Action <TOwner, TValue> setter = null)
            where TOwner : PerspexObject
        {
            Contract.Requires <ArgumentNullException>(name != null);

            PerspexProperty <TValue> result = new PerspexProperty <TValue>(
                name,
                typeof(TOwner),
                Cast(getter),
                Cast(setter));

            PerspexObject.Register(typeof(TOwner), result);

            return(result);
        }
        /// <summary>
        /// Registers an attached <see cref="PerspexProperty"/>.
        /// </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>
        /// <returns>A <see cref="PerspexProperty{TValue}"/></returns>
        public static PerspexProperty <TValue> RegisterAttached <TOwner, THost, TValue>(
            string name,
            TValue defaultValue            = default(TValue),
            bool inherits                  = false,
            BindingMode defaultBindingMode = BindingMode.OneWay)
            where TOwner : PerspexObject
        {
            Contract.Requires <NullReferenceException>(name != null);

            PerspexProperty <TValue> result = new PerspexProperty <TValue>(
                typeof(TOwner) + "." + name,
                typeof(TOwner),
                defaultValue,
                inherits,
                defaultBindingMode);

            PerspexObject.Register(typeof(THost), result);

            return(result);
        }
Beispiel #11
0
        /// <summary>
        /// Registers an attached <see cref="PerspexProperty"/>.
        /// </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="PerspexProperty{TValue}"/></returns>
        public static PerspexProperty <TValue> RegisterAttached <TOwner, THost, TValue>(
            string name,
            TValue defaultValue            = default(TValue),
            bool inherits                  = false,
            BindingMode defaultBindingMode = BindingMode.OneWay,
            Func <PerspexObject, TValue, TValue> validate = null)
        {
            Contract.Requires <ArgumentNullException>(name != null);

            PerspexProperty <TValue> result = new PerspexProperty <TValue>(
                name,
                typeof(TOwner),
                defaultValue,
                inherits,
                defaultBindingMode,
                validate,
                null,
                true);

            PerspexObject.Register(typeof(THost), result);

            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// Gets all <see cref="PerspexProperty"/>s registered on a object.
        /// </summary>
        /// <param name="o">The object.</param>
        /// <returns>A collection of <see cref="PerspexProperty"/> definitions.</returns>
        public IEnumerable <PerspexProperty> GetRegistered(PerspexObject o)
        {
            Contract.Requires <ArgumentNullException>(o != null);

            return(GetRegistered(o.GetType()));
        }
Beispiel #13
0
 /// <summary>
 /// Finds a registered property on an object by name.
 /// </summary>
 /// <param name="o">The object.</param>
 /// <param name="name">
 /// The property name. If an attached property it should be in the form
 /// "OwnerType.PropertyName".
 /// </param>
 /// <returns>
 /// The registered property or null if no matching property found.
 /// </returns>
 public PerspexProperty FindRegistered(PerspexObject o, string name)
 {
     return(FindRegistered(o.GetType(), name));
 }
Beispiel #14
0
 /// <summary>
 /// Registers the property on another type.
 /// </summary>
 /// <typeparam name="TOwner">The type of the additional owner.</typeparam>
 /// <returns>The property.</returns>
 public PerspexProperty <TValue> AddOwner <TOwner>()
 {
     PerspexObject.Register(typeof(TOwner), this);
     return(this);
 }