/// <summary>
        /// Binds a property on an <see cref="IAvaloniaObject"/> to an <see cref="IBinding"/>.
        /// </summary>
        /// <param name="target">The object.</param>
        /// <param name="property">The property to bind.</param>
        /// <param name="binding">The 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 Bind(
            this IAvaloniaObject target,
            AvaloniaProperty property,
            IBinding binding,
            object anchor = null)
        {
            Contract.Requires <ArgumentNullException>(target != null);
            Contract.Requires <ArgumentNullException>(property != null);
            Contract.Requires <ArgumentNullException>(binding != null);

            var metadata = property.GetMetadata(target.GetType()) as IDirectPropertyMetadata;

            var result = binding.Initiate(
                target,
                property,
                anchor,
                metadata?.EnableDataValidation ?? false);

            if (result != null)
            {
                return(BindingOperations.Apply(target, property, result, anchor));
            }
            else
            {
                return(Disposable.Empty);
            }
        }
Пример #2
0
        internal static object ResolveBinding(XamlBinding binding, XamlControl source, int position)
        {
#if AVALONIA
            var instancedBinding = binding.Initiate(source, _properties[position]);
            BindingOperations.Apply(source, _properties[position], instancedBinding, null);
#else
            if (!System.Windows.Data.BindingOperations.IsDataBound(source, _properties[position]))
            {
                System.Windows.Data.BindingOperations.SetBinding(source, _properties[position], binding);
            }
#endif
            return(source.GetValue(_properties[position]));
        }
Пример #3
0
        /// <summary>
        /// Applies the setter to a control.
        /// </summary>
        /// <param name="style">The style that is being applied.</param>
        /// <param name="control">The control.</param>
        /// <param name="activator">An optional activator.</param>
        public IDisposable Apply(IStyle style, IStyleable control, IObservable <bool> activator)
        {
            Contract.Requires <ArgumentNullException>(control != null);

            var description = style?.ToString();

            if (Property == null)
            {
                throw new InvalidOperationException("Setter.Property must be set.");
            }

            var value   = Value;
            var binding = value as IBinding;

            if (binding == null)
            {
                var  template = value as ITemplate;
                bool isPropertyOfTypeITemplate = typeof(ITemplate).GetTypeInfo()
                                                 .IsAssignableFrom(Property.PropertyType.GetTypeInfo());

                if (template != null && !isPropertyOfTypeITemplate)
                {
                    var materialized = template.Build();
                    NameScope.SetNameScope((Visual)materialized, new NameScope());
                    value = materialized;
                }

                if (activator == null)
                {
                    return(control.Bind(Property, ObservableEx.SingleValue(value), BindingPriority.Style));
                }
                else
                {
                    var activated = new ActivatedValue(activator, value, description);
                    return(control.Bind(Property, activated, BindingPriority.StyleTrigger));
                }
            }
            else
            {
                var source = binding.Initiate(control, Property);

                if (source != null)
                {
                    var cloned = Clone(source, style, activator);
                    return(BindingOperations.Apply(control, Property, cloned, null));
                }
            }

            return(Disposable.Empty);
        }
Пример #4
0
        /// <summary>
        /// Gets or sets a binding for a <see cref="PerspexProperty"/>.
        /// </summary>
        /// <param name="binding">The binding information.</param>
        public IObservable <object> this[IndexerDescriptor binding]
        {
            get
            {
                return(CreateBindingDescriptor(binding));
            }

            set
            {
                var metadata = binding.Property.GetMetadata(GetType());

                var mode = (binding.Mode == BindingMode.Default) ?
                           metadata.DefaultBindingMode :
                           binding.Mode;
                var sourceBinding = value as IndexerDescriptor;

                if (sourceBinding == null && mode > BindingMode.OneWay)
                {
                    mode = BindingMode.OneWay;
                }

                switch (mode)
                {
                case BindingMode.Default:
                case BindingMode.OneWay:
                    Bind(binding.Property, value, binding.Priority);
                    break;

                case BindingMode.OneTime:
                    SetValue(binding.Property, sourceBinding.Source.GetValue(sourceBinding.Property), binding.Priority);
                    break;

                case BindingMode.OneWayToSource:
                    sourceBinding.Source.Bind(sourceBinding.Property, this.GetObservable(binding.Property), binding.Priority);
                    break;

                case BindingMode.TwoWay:
                    var subject   = sourceBinding.Source.GetSubject(sourceBinding.Property, sourceBinding.Priority);
                    var instanced = new InstancedBinding(subject, BindingMode.TwoWay, sourceBinding.Priority);
                    BindingOperations.Apply(this, binding.Property, instanced, null);
                    break;
                }
            }
        }
Пример #5
0
        /// <inheritdoc/>
        protected override IControl?CreateContainer(object?item)
        {
            var container = item as T;

            if (item == null)
            {
                return(null);
            }
            else if (container != null)
            {
                Index?.Add(item, container);
                return(container);
            }
            else
            {
                var template = GetTreeDataTemplate(item, ItemTemplate);
                var result   = new T();

                if (ItemContainerTheme != null)
                {
                    result.SetValue(Control.ThemeProperty, ItemContainerTheme, BindingPriority.Style);
                }

                result.SetValue(ContentProperty, template.Build(item), BindingPriority.Style);

                var itemsSelector = template.ItemsSelector(item);

                if (itemsSelector != null)
                {
                    BindingOperations.Apply(result, ItemsProperty, itemsSelector, null);
                }

                if (!(item is IControl))
                {
                    result.DataContext = item;
                }

                Index?.Add(item, result);

                return(result);
            }
        }
Пример #6
0
        private static ICellEditBinding BindEditingElement(IAvaloniaObject target, AvaloniaProperty property, IBinding binding)
        {
            var result = binding.Initiate(target, property, enableDataValidation: true);

            if (result != null)
            {
                if (result.Subject != null)
                {
                    var bindingHelper   = new CellEditBinding(result.Subject);
                    var instanceBinding = new InstancedBinding(bindingHelper.InternalSubject, result.Mode, result.Priority);

                    BindingOperations.Apply(target, property, instanceBinding, null);
                    return(bindingHelper);
                }

                BindingOperations.Apply(target, property, result, null);
            }

            return(null);
        }
Пример #7
0
        /// <summary>
        /// Binds a property on an <see cref="IAvaloniaObject"/> to an <see cref="IBinding"/>.
        /// </summary>
        /// <param name="target">The object.</param>
        /// <param name="property">The property to bind.</param>
        /// <param name="binding">The 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 Bind(
            this IAvaloniaObject target,
            AvaloniaProperty property,
            IBinding binding,
            object anchor = null)
        {
            Contract.Requires <ArgumentNullException>(target != null);
            Contract.Requires <ArgumentNullException>(property != null);
            Contract.Requires <ArgumentNullException>(binding != null);

            var result = binding.Initiate(target, property, anchor);

            if (result != null)
            {
                return(BindingOperations.Apply(target, property, result, anchor));
            }
            else
            {
                return(Disposable.Empty);
            }
        }
Пример #8
0
        private void BindToTemplatedParent(AvaloniaProperty target, AvaloniaProperty source)
        {
            //if (!HasNonDefaultValue(target))
            {
                //Binding binding = new Binding();
                //binding.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
                //binding.Path = source.p//new PropertyPath(source);
                //SetBinding(target, binding);
                try
                {
                    var sourceBinding = this.GetSubject(source);

                    InstancedBinding instancedBinding = new InstancedBinding(sourceBinding, BindingMode.TwoWay, BindingPriority.TemplatedParent);
                    BindingOperations.Apply(this, target, instancedBinding, TemplatedParent);
                }
                catch (Exception ex)
                {
                }
                //Bind(target, ObservableEx.SingleValue(source));
            }
        }
Пример #9
0
        /// <summary>
        /// tries to create a container from this item
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        protected override IControl CreateContainer(object item)
        {
            var container = item as IndexListHeaderItem;

            if (item == null)
            {
                return(null);
            }
            else if (container != null)
            {
                return(container);
            }
            else
            {
                IndexList owner = Owner as IndexList;

                var template = GetTreeDataTemplate(item, ItemTemplate);
                var result   = new IndexListHeaderItem();

                result.SetValue(ContentProperty, template.Build(item), BindingPriority.Style);
                Binding binding = new Binding();
                binding.Source = owner;
                binding.Path   = nameof(owner.ShowEmptyItems);
                result.Bind(IndexListHeaderItem.ShowEmptyItemsProperty, binding, BindingPriority.Style);

                var itemsSelector = template.ItemsSelector(item);

                if (itemsSelector != null)
                {
                    BindingOperations.Apply(result, ItemsProperty, itemsSelector, null);
                }

                if ((item is IControl) == false)
                {
                    result.DataContext = item;
                }

                return(result);
            }
        }
Пример #10
0
        /// <inheritdoc/>
        protected override IControl CreateContainer(object item)
        {
            var container = item as T;

            if (item == null)
            {
                return(null);
            }
            else if (container != null)
            {
                Index.Add(item, container);
                return(container);
            }
            else
            {
                var template = GetTreeDataTemplate(item, ItemTemplate);
                var result   = new T();

                result.SetValue(ContentProperty, template.Build(item));

                var itemsSelector = template.ItemsSelector(item);

                if (itemsSelector != null)
                {
                    BindingOperations.Apply(result, ItemsProperty, itemsSelector, null);
                }

                if (!(item is IControl))
                {
                    result.DataContext = item;
                }

                NameScope.SetNameScope((Control)(object)result, new NameScope());
                Index.Add(item, result);

                return(result);
            }
        }
Пример #11
0
        /// <summary>
        /// Applies the setter to a control.
        /// </summary>
        /// <param name="style">The style that is being applied.</param>
        /// <param name="control">The control.</param>
        /// <param name="activator">An optional activator.</param>
        public IDisposable Apply(IStyle style, IStyleable control, IObservable <bool> activator)
        {
            Contract.Requires <ArgumentNullException>(control != null);

            var description = style?.ToString();

            if (Property == null)
            {
                throw new InvalidOperationException("Setter.Property must be set.");
            }

            var binding = Value as IBinding;

            if (binding == null)
            {
                if (activator == null)
                {
                    return(control.Bind(Property, ObservableEx.SingleValue(Value), BindingPriority.Style));
                }
                else
                {
                    var activated = new ActivatedValue(activator, Value, description);
                    return(control.Bind(Property, activated, BindingPriority.StyleTrigger));
                }
            }
            else
            {
                var source = binding.Initiate(control, Property);

                if (source != null)
                {
                    var cloned = Clone(source, style, activator);
                    return(BindingOperations.Apply(control, Property, cloned, null));
                }
            }

            return(Disposable.Empty);
        }