/// <summary>
        /// Applies an <see cref="InstancedBinding"/> a property on an <see cref="IAvaloniaObject"/>.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="property">The property to bind.</param>
        /// <param name="binding">The instanced binding.</param>
        /// <param name="anchor">
        /// An optional anchor from which to locate required context. When binding to objects that
        /// are not in the logical tree, certain types of binding need an anchor into the tree in
        /// order to locate named controls or resources. The <paramref name="anchor"/> parameter
        /// can be used to provide this context.
        /// </param>
        /// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
        public static IDisposable Apply(
            IAvaloniaObject target,
            AvaloniaProperty property,
            InstancedBinding binding,
            object anchor)
        {
            Contract.Requires <ArgumentNullException>(target != null);
            Contract.Requires <ArgumentNullException>(property != null);
            Contract.Requires <ArgumentNullException>(binding != null);

            var mode = binding.Mode;

            if (mode == BindingMode.Default)
            {
                mode = property.GetMetadata(target.GetType()).DefaultBindingMode;
            }

            switch (mode)
            {
            case BindingMode.Default:
            case BindingMode.OneWay:
                return(target.Bind(property, binding.Observable ?? binding.Subject, binding.Priority));

            case BindingMode.TwoWay:
                return(new CompositeDisposable(
                           target.Bind(property, binding.Subject, binding.Priority),
                           target.GetObservable(property).Subscribe(binding.Subject)));

            case BindingMode.OneTime:
                var source = binding.Subject ?? binding.Observable;

                if (source != null)
                {
                    return(source
                           .Where(x => BindingNotification.ExtractValue(x) != AvaloniaProperty.UnsetValue)
                           .Take(1)
                           .Subscribe(x => target.SetValue(property, x, binding.Priority)));
                }
                else
                {
                    target.SetValue(property, binding.Value, binding.Priority);
                    return(Disposable.Empty);
                }

            case BindingMode.OneWayToSource:
                return(Observable.CombineLatest(
                           binding.Observable,
                           target.GetObservable(property),
                           (_, v) => v)
                       .Subscribe(x => binding.Subject.OnNext(x)));

            default:
                throw new ArgumentException("Invalid binding mode.");
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the <see cref="BehaviorCollection"/> associated with a specified object.
        /// </summary>
        /// <param name="obj">The <see cref="IAvaloniaObject"/> from which to retrieve the <see cref="BehaviorCollection"/>.</param>
        /// <returns>A <see cref="BehaviorCollection"/> containing the behaviors associated with the specified object.</returns>
        public static BehaviorCollection?GetBehaviors(IAvaloniaObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            BehaviorCollection?behaviorCollection = (BehaviorCollection?)obj.GetValue(BehaviorsProperty);

            if (behaviorCollection == null)
            {
                behaviorCollection = new BehaviorCollection();
                obj.SetValue(BehaviorsProperty, behaviorCollection);

                if (obj is Control control)
                {
                    control.AttachedToVisualTree   -= Control_Loaded;
                    control.AttachedToVisualTree   += Control_Loaded;
                    control.DetachedFromVisualTree -= Control_Unloaded;
                    control.DetachedFromVisualTree += Control_Unloaded;
                }
            }

            return(behaviorCollection);
        }
예제 #3
0
        /// <summary>
        /// Applies a binding subject to a property on an instance.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="property">The target property.</param>
        /// <param name="subject">The binding subject.</param>
        internal void Bind(IAvaloniaObject target, AvaloniaProperty property, ISubject <object> subject)
        {
            var mode = Mode == BindingMode.Default ?
                       property.GetMetadata(target.GetType()).DefaultBindingMode : Mode;

            switch (mode)
            {
            case BindingMode.Default:
            case BindingMode.OneWay:
                target.Bind(property, subject, Priority);
                break;

            case BindingMode.TwoWay:
                throw new NotSupportedException("TwoWay MultiBinding not currently supported.");

            case BindingMode.OneTime:
                target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                {
                    subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority));
                });
                break;

            case BindingMode.OneWayToSource:
                target.GetObservable(property).Subscribe(subject);
                break;
            }
        }
예제 #4
0
        /// <summary>
        /// Applies an <see cref="InstancedBinding"/> a property on an <see cref="IAvaloniaObject"/>.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="property">The property to bind.</param>
        /// <param name="binding">The instanced binding.</param>
        /// <param name="anchor">
        /// An optional anchor from which to locate required context. When binding to objects that
        /// are not in the logical tree, certain types of binding need an anchor into the tree in 
        /// order to locate named controls or resources. The <paramref name="anchor"/> parameter 
        /// can be used to provice this context.
        /// </param>
        /// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
        public static IDisposable Apply(
            IAvaloniaObject target,
            AvaloniaProperty property,
            InstancedBinding binding,
            object anchor)
        {
            Contract.Requires<ArgumentNullException>(target != null);
            Contract.Requires<ArgumentNullException>(property != null);
            Contract.Requires<ArgumentNullException>(binding != null);

            var mode = binding.Mode;

            if (mode == BindingMode.Default)
            {
                mode = property.GetMetadata(target.GetType()).DefaultBindingMode;
            }

            switch (mode)
            {
                case BindingMode.Default:
                case BindingMode.OneWay:
                    return target.Bind(property, binding.Observable ?? binding.Subject, binding.Priority);
                case BindingMode.TwoWay:
                    return new CompositeDisposable(
                        target.Bind(property, binding.Subject, binding.Priority),
                        target.GetObservable(property).Subscribe(binding.Subject));
                case BindingMode.OneTime:
                    var source = binding.Subject ?? binding.Observable;

                    if (source != null)
                    {
                        return source
                            .Where(x => BindingNotification.ExtractValue(x) != AvaloniaProperty.UnsetValue)
                            .Take(1)
                            .Subscribe(x => target.SetValue(property, x, binding.Priority));
                    }
                    else
                    {
                        target.SetValue(property, binding.Value, binding.Priority);
                        return Disposable.Empty;
                    }
                case BindingMode.OneWayToSource:
                    return target.GetObservable(property).Subscribe(binding.Subject);
                default:
                    throw new ArgumentException("Invalid binding mode.");
            }
        }
예제 #5
0
 public static void SetSyntax(IAvaloniaObject obj, string value)
 {
     obj.SetValue(SyntaxProperty, value);
     if (obj is TextEditor textEditor)
     {
         textEditor.SyntaxHighlighting = AvaloniaEditSyntaxManager.Instance.GetDefinition(value);
     }
 }
예제 #6
0
 /// <summary>
 /// Sets the <see cref="BehaviorCollection"/> associated with a specified object.
 /// </summary>
 /// <param name="obj">The <see cref="IAvaloniaObject"/> on which to set the <see cref="BehaviorCollection"/>.</param>
 /// <param name="value">The <see cref="BehaviorCollection"/> associated with the object.</param>
 public static void SetBehaviors(IAvaloniaObject obj, BehaviorCollection?value)
 {
     if (obj == null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     obj.SetValue(BehaviorsProperty, value);
 }
예제 #7
0
 /// <summary>
 /// Gets a subject for a <see cref="AvaloniaProperty"/>.
 /// </summary>
 /// <typeparam name="T">The property type.</typeparam>
 /// <param name="o">The object.</param>
 /// <param name="property">The property.</param>
 /// <param name="priority">
 /// The priority with which binding values are written to the object.
 /// </param>
 /// <returns>
 /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the
 /// property.
 /// </returns>
 public static ISubject <T> GetSubject <T>(
     this IAvaloniaObject o,
     AvaloniaProperty <T> property,
     BindingPriority priority = BindingPriority.LocalValue)
 {
     return(Subject.Create <T>(
                Observer.Create <T>(x => o.SetValue(property, x, priority)),
                o.GetObservable(property)));
 }
예제 #8
0
        /// <summary>
        /// Sets the <see cref="RegionManagerProperty"/> attached property.
        /// </summary>
        /// <param name="target">The target element.</param>
        /// <param name="value">The value.</param>
        public static void SetRegionManager(IAvaloniaObject target, IRegionManager value)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            target.SetValue(RegionManagerProperty, value);
        }
예제 #9
0
 /// <summary>
 /// Gets a subject for a <see cref="AvaloniaProperty"/>.
 /// </summary>
 /// <typeparam name="T">The property type.</typeparam>
 /// <param name="o">The object.</param>
 /// <param name="property">The property.</param>
 /// <param name="priority">
 /// The priority with which binding values are written to the object.
 /// </param>
 /// <returns>
 /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the
 /// property.
 /// </returns>
 public static ISubject <BindingValue <T> > GetBindingSubject <T>(
     this IAvaloniaObject o,
     AvaloniaProperty <T> property,
     BindingPriority priority = BindingPriority.LocalValue)
 {
     return(Subject.Create <BindingValue <T> >(
                Observer.Create <BindingValue <T> >(x =>
     {
         if (x.HasValue)
         {
             o.SetValue(property, x.Value, priority);
         }
     }),
                o.GetBindingObservable(property)));
 }
예제 #10
0
        internal override void RouteSetValue(
            IAvaloniaObject o,
            object value,
            BindingPriority priority)
        {
            var v = TryConvert(value);

            if (v.HasValue)
            {
                o.SetValue <TValue>(this, (TValue)v.Value);
            }
            else if (v.Type == BindingValueType.UnsetValue)
            {
                o.ClearValue(this);
            }
            else if (v.HasError)
            {
                throw v.Error !;
            }
        }
예제 #11
0
        /// <inheritdoc/>
        internal override IDisposable?RouteSetValue(
            IAvaloniaObject o,
            object?value,
            BindingPriority priority)
        {
            var v = TryConvert(value);

            if (v.HasValue)
            {
                return(o.SetValue <TValue>(this, (TValue)v.Value !, priority));
            }
            else if (v.Type == BindingValueType.UnsetValue)
            {
                o.ClearValue(this);
            }
            else if (v.HasError)
            {
                throw v.Error !;
            }

            return(null);
        }
 public static void SetClass(IAvaloniaObject element, string value) => element.SetValue(ClassProperty, value);
 public static void SetBorderPresence(IAvaloniaObject obj, BorderPresence value)
 {
     obj.SetValue(BorderPresenceProperty, value);
 }
예제 #14
0
 public static void SetSyntaxExtension(IAvaloniaObject obj, string value)
 {
     obj.SetValue(SyntaxExtensionProperty, value);
 }
 public static void SetColumnsWithHeader(IAvaloniaObject obj, ObservableCollection <DatabaseColumnHeaderViewModel> value)
 {
     obj.SetValue(ColumnsWithHeaderProperty, value);
 }
예제 #16
0
 public static void SetDropHandler(IAvaloniaObject obj, IDropTarget?value)
 {
     obj.SetValue(DropHandlerProperty, value);
 }
예제 #17
0
 public static void SetIsDragSource(IAvaloniaObject obj, bool value)
 {
     obj.SetValue(IsDragSourceProperty, value);
 }
예제 #18
0
 public static void SetIcon(IAvaloniaObject obj, object value)
 {
     obj.SetValue(IconProperty, value);
 }
 public static void SetTrigger(IAvaloniaObject element, bool value) => element.SetValue(TriggerProperty, value);
예제 #20
0
        /// <summary>
        /// Applies an <see cref="InstancedBinding"/> a property on an <see cref="IAvaloniaObject"/>.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="property">The property to bind.</param>
        /// <param name="binding">The instanced binding.</param>
        /// <param name="anchor">
        /// An optional anchor from which to locate required context. When binding to objects that
        /// are not in the logical tree, certain types of binding need an anchor into the tree in
        /// order to locate named controls or resources. The <paramref name="anchor"/> parameter
        /// can be used to provide this context.
        /// </param>
        /// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
        public static IDisposable Apply(
            IAvaloniaObject target,
            AvaloniaProperty property,
            InstancedBinding binding,
            object?anchor)
        {
            _ = target ?? throw new ArgumentNullException(nameof(target));
            _ = property ?? throw new ArgumentNullException(nameof(property));
            _ = binding ?? throw new ArgumentNullException(nameof(binding));

            var mode = binding.Mode;

            if (mode == BindingMode.Default)
            {
                mode = property.GetMetadata(target.GetType()).DefaultBindingMode;
            }

            switch (mode)
            {
            case BindingMode.Default:
            case BindingMode.OneWay:
                if (binding.Observable is null)
                {
                    throw new InvalidOperationException("InstancedBinding does not contain an observable.");
                }
                return(target.Bind(property, binding.Observable, binding.Priority));

            case BindingMode.TwoWay:
                if (binding.Subject is null)
                {
                    throw new InvalidOperationException("InstancedBinding does not contain a subject.");
                }
                return(new TwoWayBindingDisposable(
                           target.Bind(property, binding.Subject, binding.Priority),
                           target.GetObservable(property).Subscribe(binding.Subject)));

            case BindingMode.OneTime:
                var source = binding.Subject ?? binding.Observable;

                if (source != null)
                {
                    // Perf: Avoid allocating closure in the outer scope.
                    var targetCopy   = target;
                    var propertyCopy = property;
                    var bindingCopy  = binding;

                    return(source
                           .Where(x => BindingNotification.ExtractValue(x) != AvaloniaProperty.UnsetValue)
                           .Take(1)
                           .Subscribe(x => targetCopy.SetValue(
                                          propertyCopy,
                                          BindingNotification.ExtractValue(x),
                                          bindingCopy.Priority)));
                }
                else
                {
                    target.SetValue(property, binding.Value, binding.Priority);
                    return(Disposable.Empty);
                }

            case BindingMode.OneWayToSource:
            {
                if (binding.Observable is null)
                {
                    throw new InvalidOperationException("InstancedBinding does not contain an observable.");
                }
                if (binding.Subject is null)
                {
                    throw new InvalidOperationException("InstancedBinding does not contain a subject.");
                }

                // Perf: Avoid allocating closure in the outer scope.
                var bindingCopy = binding;

                return(Observable.CombineLatest(
                           binding.Observable,
                           target.GetObservable(property),
                           (_, v) => v)
                       .Subscribe(x => bindingCopy.Subject.OnNext(x)));
            }

            default:
                throw new ArgumentException("Invalid binding mode.");
            }
        }
예제 #21
0
        /// <summary>
        /// Applies a binding subject to a property on an instance.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="property">The target property.</param>
        /// <param name="subject">The binding subject.</param>
        internal void Bind(IAvaloniaObject target, AvaloniaProperty property, ISubject<object> subject)
        {
            var mode = Mode == BindingMode.Default ?
                property.GetMetadata(target.GetType()).DefaultBindingMode : Mode;

            switch (mode)
            {
                case BindingMode.Default:
                case BindingMode.OneWay:
                    target.Bind(property, subject, Priority);
                    break;
                case BindingMode.TwoWay:
                    throw new NotSupportedException("TwoWay MultiBinding not currently supported.");
                case BindingMode.OneTime:
                    target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                    {
                        subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority));
                    });                    
                    break;
                case BindingMode.OneWayToSource:
                    target.GetObservable(property).Subscribe(subject);
                    break;
            }
        }
예제 #22
0
 public static void SetCornerCurves(IAvaloniaObject obj, CornerCurves value)
 {
     obj.SetValue(CornerCurvesProperty, value);
 }
예제 #23
0
 public static void SetIconGap(IAvaloniaObject obj, double value)
 {
     obj.SetValue(IconGapProperty, value);
 }
예제 #24
0
 public static void SetIcon(IAvaloniaObject obj, IControlTemplate value)
 {
     obj.SetValue(IconProperty, value);
 }
예제 #25
0
 public static void SetIsDropTarget(IAvaloniaObject obj, bool value)
 {
     obj.SetValue(IsDropTargetProperty, value);
 }
예제 #26
0
 public static void SetAutoWireViewModel(IAvaloniaObject obj, bool value)
 {
     obj.SetValue(AutoWireViewModelProperty, value);
 }