示例#1
0
        /// <inheritdoc/>
        public InstancedBinding?Initiate(
            IAvaloniaObject target,
            AvaloniaProperty?targetProperty,
            object?anchor             = null,
            bool enableDataValidation = false)
        {
            // Usually each `TemplateBinding` will only be instantiated once; in this case we can
            // use the `TemplateBinding` object itself as the instanced binding in order to save
            // allocating a new object.
            //
            // If the binding appears in a `Setter`, then make a clone and instantiate that because
            // because the setter can outlive the control and cause a leak.
            if (_target == null && !_isSetterValue)
            {
                _target     = (IStyledElement)target;
                _targetType = targetProperty?.PropertyType;

                return(new InstancedBinding(
                           this,
                           Mode == BindingMode.Default ? BindingMode.OneWay : Mode,
                           BindingPriority.TemplatedParent));
            }
            else
            {
                var clone = new TemplateBinding
                {
                    Converter          = Converter,
                    ConverterParameter = ConverterParameter,
                    Property           = Property,
                };

                return(clone.Initiate(target, targetProperty, anchor, enableDataValidation));
            }
        }
示例#2
0
        /// <inheritdoc/>
        public InstancedBinding Initiate(
            IAvaloniaObject target,
            AvaloniaProperty targetProperty,
            object anchor             = null,
            bool enableDataValidation = false)
        {
            // Usually each `TemplateBinding` will only be instantiated once; in this case we can
            // use the `TemplateBinding` object itself as the instanced binding in order to save
            // allocating a new object. If the binding *is* instantiated more than once (which can
            // happen if it appears in a `Setter` for example, then just make a clone and instantiate
            // that.
            if (_target == null)
            {
                _target     = (IStyledElement)target;
                _targetType = targetProperty?.PropertyType;

                return(new InstancedBinding(
                           this,
                           Mode == BindingMode.Default ? BindingMode.OneWay : Mode,
                           BindingPriority.TemplatedParent));
            }
            else
            {
                var clone = new TemplateBinding
                {
                    Converter          = Converter,
                    ConverterParameter = ConverterParameter,
                    Property           = Property,
                };

                return(clone.Initiate(target, targetProperty, anchor, enableDataValidation));
            }
        }