예제 #1
0
파일: Bindable.cs 프로젝트: piRepos/pEngine
        /// <summary>
        /// Creates a data binding between this property and another which
        /// have a different type.
        /// </summary>
        /// <typeparam name="T2">Target property type.</typeparam>
        /// <param name="property">Binding target property.</param>
        /// <param name="outAdapter">Data conversion function.</param>
        public void Bind <T2>(Bindable <T2> property, Func <T, T2> outAdapter)
        {
            if (outAdapter == null)
            {
                throw new InvalidOperationException("No binding adapter provided.");
            }

            if (!AllowedDirections.HasFlag(BindingDirection.Read))
            {
                throw new InvalidOperationException("This binding direction is not allowed.");
            }

            // - Get a weak reference of the property
            var propRef = new WeakReference <Bindable <T2> >(property);

            var bindingAction = new Action(() =>
            {
                // - Check if the target property is still alive
                if (!propRef.TryGetTarget(out Bindable <T2> prop))
                {
                    Bindings.Remove(propRef);
                    return;
                }

                // - Update value
                prop.Value = outAdapter.Invoke(InternalValue);
            });

            Bindings.Add(propRef, bindingAction);
        }
예제 #2
0
파일: Bindable.cs 프로젝트: piRepos/pEngine
        /// <summary>
        /// Creates a data binding between this property and the specified one.
        /// </summary>
        /// <param name="property">Binding target property.</param>
        /// <param name="direction">Binding direction.</param>
        public void Bind(Bindable <T> property, BindingDirection direction = BindingDirection.Both)
        {
            bool invalidOperation = false;

            invalidOperation = invalidOperation || direction.HasFlag(BindingDirection.Read) && !AllowedDirections.HasFlag(BindingDirection.Write);
            invalidOperation = invalidOperation || direction.HasFlag(BindingDirection.Write) && !AllowedDirections.HasFlag(BindingDirection.Read);

            if (invalidOperation)
            {
                throw new InvalidOperationException("This binding direction is not allowed");
            }

            // - Get a weak reference of the property
            var propRef = new WeakReference <Bindable <T> >(property);

            if (direction.HasFlag(BindingDirection.Write))
            {
                var bindingAction = new Action(() =>
                {
                    // - Check if the target property is still alive
                    if (!propRef.TryGetTarget(out Bindable <T> prop))
                    {
                        Bindings.Remove(propRef);
                        return;
                    }

                    // - Update value
                    prop.Value = InternalValue;
                });

                Bindings.Add(propRef, bindingAction);
            }

            // - If we want to read the target property, delegate binding process
            if (direction.HasFlag(BindingDirection.Read))
            {
                property.Bind(this, BindingDirection.Write);
            }
        }