/// <summary>
        /// Bind to a command when the TargetProperty is known
        /// </summary>
        /// <param name="control"></param>
        /// <param name="bindingOptions"></param>
        /// <param name="targetPropertyName"></param>
        public void ExecuteCommandBind(IBindingTarget control, Options bindingOptions, string targetPropertyName)
        {
            BindingDef binding = new BindingDef(control, targetPropertyName, bindingOptions, controlService);

            CommandStorage.Add(binding);
            ExecuteCommandBind(control, binding);
        }
        public bool TryGetTargetPropertyName(IBindingTarget target, Options options, out string targetPropertyName)
        {
            targetPropertyName = string.Empty;
            try
            {
                //get method body
                StackFrame frame      = GetBindingFrame();
                string     methodName = frame.GetMethod().Name;

                if (!this.SequenceManager.Sequences.Keys.Contains(methodName))
                {
                    this.SequenceManager.Sequences.Add(methodName, new BindingSequnce()
                    {
                        Instructions = GetBindingInstructionSets(frame)
                    });
                }
                else
                {
                    this.SequenceManager.Sequences[methodName].MoveNext();
                }

                targetPropertyName = this.SequenceManager.Sequences[methodName].CurrentInstruction.GetAssignedProperty();
            }
            catch (Exception exp)
            {
                Logger.Trace(exp);
            }

            return(!string.IsNullOrEmpty(targetPropertyName));
        }
        public static void To <T, TP>(this IBindingTarget target, T model, Expression <Func <T, TP> > action)
            where T : class
        {
            var expr = (MemberExpression)action.Body;

            target.Control.DataBindings.Add(target.PropertyName, model, expr.Member.Name);
        }
Example #4
0
        public void Connect(Gtk.Widget widget, Object source, IBindingTarget target)
        {
            if (source == null)
            {
                mSource = BindingEngine.GetViewModel(widget);
                if (mSource == null)
                {
                    Console.WriteLine("Binding.Connect: ViewModel not set for this widget");
                    return;
                }
            }
            else
            {
                mSource = source;
            }

            mTarget = target;

            var propertyChanged = mSource as IPropertyChanged;

            if (propertyChanged != null)
            {
                propertyChanged.PropertyChanged += (object sender, PropertyChangedEventArgs e) => mTarget.Update(this, GetSourceValue());
            }
        }
Example #5
0
        /// <summary>
        /// Sets the current target.
        /// </summary>
        /// <remarks>
        /// This will clear current hierarchy and regenerate a new one.
        /// </remarks>
        /// <param name="target">The target to set.</param>
        /// <typeparam name="T">The type of the target.</typeparam>
        public void SetTarget <T>(T target)
        {
            if (TryGetTarget(out T current) && current.Equals(target))
            {
                // Nothing to do..
                return;
            }

            if (null != m_BindingTarget)
            {
                var type = typeof(T);
                if (typeof(T).IsClass && null != target)
                {
                    type = target.GetType();
                }

                if (type == m_BindingTarget.TargetType)
                {
                    (m_BindingTarget as BindingTarget <T>).Target = target;
                    return;
                }
            }

            m_BindingTarget?.Release();

            if (typeof(T).IsClass && null == target)
            {
                m_BindingTarget = null;
                return;
            }

            m_BindingTarget = new BindingTarget <T>(this, target);
            name            = target.GetType().Name;
            m_BindingTarget.GenerateHierarchy();
        }
        private bool DetermineIfCollection(IBindingTarget control, object value)
        {
            if (value is IEnumerable && !(value is string))
            {
                return(true);
            }

            return(false);
        }
Example #7
0
        /// <summary>
        /// Used to create a command binding
        /// </summary>
        /// <param name="sourceExpression"></param>
        /// <param name="targetExpression"></param>
        /// <param name="containerID"></param>
        public BindingDef(IBindingTarget targetControl, string targetPropertyName, Options bindingOptions, IControlService controlService)
        {
            this.controlService   = controlService;
            this.sourceExpression = new BindingPath(bindingOptions.Path);

            string targetExpression = string.Format("{0}.{1}", targetControl.UniqueID, targetPropertyName);

            this.targetExpression = new BindingPath(targetExpression);
        }
        /// <summary>
        /// Bind to a command with auto resolution of the TargetProperty
        /// </summary>
        /// <param name="sourceExpression"></param>
        /// <param name="targetExpression"></param>
        public void ExecuteCommandBind(IBindingTarget control, Options bindingOptions)
        {
            string propertyName = string.Empty;

            if (controlService.TryGetTargetPropertyName(control, bindingOptions, out propertyName))
            {
                ExecuteCommandBind(control, bindingOptions, propertyName);
            }
        }
        private object AttemptConvert(BindingDef binding, object value, IBindingTarget container)
        {
            if (binding.HasValueConverter)
            {
                IValueConverter converter = binding.GetValueConverterInstance();
                value = converter.Convert(value, binding.ResolveAsTarget(container).Descriptor.PropertyType, binding, CultureInfo.CurrentCulture);
            }

            return(value);
        }
        public IEnumerable GetChildren(IBindingTarget parent)
        {
            Control ctrl = Resolve(parent);

            if (ctrl == null)
            {
                throw new ArgumentException("parent must be of type System.Web.UI.Control in order for its children to be resolved by this service", "parent");
            }

            return(ctrl.Controls);
        }
        public IBindingTarget GetParent(IBindingTarget child)
        {
            Control ctrl = Resolve(child);

            if (ctrl == null)
            {
                throw new ArgumentException("child must be of type System.Web.UI.Control in order for its parent to be resolved by this service", "child");
            }

            return(new WebformControl(ctrl.Parent));
        }
Example #12
0
 internal Component(Type type,
                    ILiftTimeScope liftTimeScope,
                    string name,
                    IBindingTarget bingingTarget
                    )
 {
     this.target        = type;
     this.name          = name;
     this.bingingTarget = bingingTarget;
     this.liftTimeScope = liftTimeScope;
 }
        /// <summary>
        /// Execute a bind in the context of a collection container.
        /// </summary>
        /// <remarks>
        /// This method will attempt to auto resolve the property of the target control to which to
        /// bind. The implementation of this resolution can be control via IControlService.TryGetTargetPropertyName.
        /// </remarks>
        /// <param name="control">The target control</param>
        /// <param name="collectionItemContainer">
        /// The collection item container. E.g. In the case of a repeater a RepeaterItem.
        /// This is available from the ItemTemplate control via (IDataItemContainer)container.NamingContainer.
        /// </param>
        /// <param name="bindingOptions"></param>
        /// <returns>The result (value) of the bind</returns>
        public object ExecuteBind(IBindingTarget control, IBindingTarget <IDataItemContainer> collectionItemContainer, Options bindingOptions)
        {
            string propertyName = string.Empty;
            object value        = null;

            if (controlService.TryGetTargetPropertyName(control, bindingOptions, out propertyName))
            {
                value = ExecuteBind(control, collectionItemContainer, bindingOptions, propertyName);
            }

            return(value);
        }
Example #14
0
        /// <summary>
        /// Used to create a data binding
        /// </summary>
        /// <param name="targetControl">The control to which to bind</param>
        /// <param name="targetPropertyName">The property of the control to which to bind</param>
        /// <param name="bindingOptions">Options to control the binding</param>
        /// <param name="isSourceEnumerable">Is the datasource to which you are binidng a collection</param>
        /// <param name="controlService">Service required for binding</param>
        public BindingDef(IBindingTarget targetControl, string targetPropertyName, Options bindingOptions, bool isSourceEnumerable, IControlService controlService)
        {
            this.controlService   = controlService;
            this.Container        = targetControl;
            this.sourceExpression = new BindingPath(bindingOptions.Path);

            string targetExpression = string.Format("{0}.{1}", targetControl.UniqueID, targetPropertyName);

            this.targetExpression = new BindingPath(targetExpression);

            this.bindingOptions     = bindingOptions;
            this.IsSourceEnumerable = isSourceEnumerable;
        }
Example #15
0
        public TargetEvent ResolveAsTargetEvent(IBindingTarget container, IControlService controlService)
        {
            string[] targetExpressionSplit = this.Raw.Split('.');

            IBindingTarget control    = controlService.FindControlUnique(container, targetExpressionSplit[0]);
            object         rawControl = controlService.Unwrap(control);

            EventInfo info = rawControl.GetType().GetEvent(targetExpressionSplit[1]);

            return(new TargetEvent {
                OwningControl = rawControl, Descriptor = info
            });
        }
Example #16
0
        public static Binding GetOrCreateBinding(Gtk.Widget widget, Object source, IBindingTarget target, BindingInfo info)
        {
            Binding result = null;

            if (!mBindings.TryGetValue(target, out result))
            {
                result      = new Binding();
                result.Info = info;
                result.Connect(widget, source, target);
                mBindings[target] = result;
            }
            return(result);
        }
        private BindingMode DetermineBindingDirection(IBindingTarget control, object value)
        {
            if (control == null)
            {
                return(BindingMode.Unsupported);
            }

            //String are IEnumerable!
            if (DetermineIfCollection(control, value))
            {
                return(BindingMode.OneWay);
            }

            return(BindingMode.TwoWay);
        }
        private Control Resolve(IBindingTarget iTarget)
        {
            Control target = iTarget as Control;

            if (target == null)
            {
                WebformControl webformControl = iTarget as WebformControl;
                if (webformControl != null)
                {
                    target = webformControl.Control;
                }
            }

            return(target);
        }
Example #19
0
        /// <summary>
        /// Used to create a nested data binding
        /// </summary>
        /// <param name="targetControl">The control to which to bind</param>
        /// <param name="targetPropertyName">The property of the control to which to bind</param>
        /// <param name="dataItemIndex">If the parent binding is a collection, the index is the data item for this binding. Else zerp</param>
        /// <param name="owningCollection">The collection of current bindings</param>
        /// <param name="bindingOptions">Options to control the binding</param>
        /// <param name="isSourceEnumerable">Is the datasource to which you are binidng a collection</param>
        /// <param name="controlService">Service required for binding</param>
        public BindingDef(IBindingTarget targetControl, string targetPropertyName, int dataItemIndex, BindingCollection owningCollection, Options bindingOptions, bool isSourceEnumerable, IControlService controlService)
        {
            this.controlService = controlService;
            this.Container      = targetControl;
            this.TrySetParent(owningCollection);
            this.sourceExpression = new BindingPath(bindingOptions.Path, dataItemIndex, GetParentExpression(), bindingOptions.PathMode);

            string targetExpression = string.Format("{0}.{1}", targetControl.UniqueID, targetPropertyName);

            this.targetExpression = new BindingPath(targetExpression);

            this.DataItemIndex      = dataItemIndex;
            this.bindingOptions     = bindingOptions;
            this.IsSourceEnumerable = isSourceEnumerable;
        }
Example #20
0
        /// <inheritdoc />
        public async Task Apply(IBindingTarget target)
        {
            if (!bindingInfo.IsObsolete)
            {
                switch (bindingInfo.BindingTargetMode)
                {
                case BindingTargetMode.Default:
                    if (bindingInfo.QueueInfo.QueueType == QueueType.Dynamic)
                    {
                        QueueName = await target.BindDynamic(bindingInfo.MessageClass, bindingInfo.QueueInfo.Name);
                    }
                    else
                    {
                        await target.BindDurable(bindingInfo.MessageClass, bindingInfo.QueueInfo.Name);

                        QueueName = bindingInfo.QueueInfo.Name;
                    }

                    break;

                case BindingTargetMode.Direct:
                    if (bindingInfo.QueueInfo.QueueType == QueueType.Dynamic)
                    {
                        QueueName = await target.BindDynamicDirect(bindingInfo.MessageClass, bindingInfo.QueueInfo.Name);
                    }
                    else
                    {
                        await target.BindDurableDirect(bindingInfo.QueueInfo.Name);

                        QueueName = bindingInfo.QueueInfo.Name;
                    }

                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(bindingInfo.BindingTargetMode), bindingInfo.BindingTargetMode, "Invalid BindingTargetMode");
                }
            }
            else if (bindingInfo.QueueInfo.QueueType == QueueType.Durable)
            {
                await target.BindDurableObsolete(bindingInfo.QueueInfo.Name);

                QueueName = bindingInfo.QueueInfo.Name;
            }
        }
Example #21
0
        private bool TrySetParent(BindingDef lastBinding)
        {
            if (lastBinding != null)
            {
                //test if the current binding is a child of the previous datacontainer
                IBindingTarget child = controlService.FindControlUnique(lastBinding.Container, this.Container.UniqueID);
                if (child != null)
                {
                    this.Parent = lastBinding;
                }
                else if (lastBinding.Parent != null)
                {
                    TrySetParent(lastBinding.Parent);
                }
            }

            return(this.Parent != null);
        }
Example #22
0
        public void Connect(Gtk.Widget widget, Object source, IBindingTarget target)
        {
            if (source == null) {
                mSource = BindingEngine.GetViewModel(widget);
                if (mSource == null) {
                    Console.WriteLine("Binding.Connect: ViewModel not set for this widget");
                    return;
                }
            } else {
                mSource = source;
            }

            mTarget = target;

            var propertyChanged = mSource as IPropertyChanged;
            if (propertyChanged != null) {
                propertyChanged.PropertyChanged += (object sender, PropertyChangedEventArgs e) => mTarget.Update(this, GetSourceValue());
            }
        }
        public IBindingTarget FindControlUnique(IBindingTarget root, string unique)
        {
            if (root.UniqueID == unique)
            {
                return(root);
            }

            foreach (Control ctl in this.GetChildren(root))
            {
                IBindingTarget webFormControl = new WebformControl(ctl);
                IBindingTarget foundCtl       = FindControlUnique(webFormControl, unique);
                if (foundCtl != null)
                {
                    return(foundCtl);
                }
            }

            return(null);
        }
        public IBindingTarget FindControlRecursive(IBindingTarget root, string id)
        {
            if (root.ID == id)
            {
                return(root);
            }

            foreach (Control ctl in this.GetChildren(root))
            {
                IBindingTarget webFormControl = new WebformControl(ctl);
                IBindingTarget foundCtl       = FindControlRecursive(webFormControl, id);
                if (foundCtl != null)
                {
                    return(foundCtl);
                }
            }

            return(null);
        }
Example #25
0
        public TargetProperty ResolveAsTarget(IBindingTarget container, IControlService controlService)
        {
            string[] targetExpressionSplit = this.Raw.Split('.');

            IBindingTarget control    = controlService.FindControlUnique(container, targetExpressionSplit[0]);
            object         rawControl = controlService.Unwrap(control);

            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(rawControl).Find(targetExpressionSplit[1], true);

            object value = null;

            if (descriptor != null)
            {
                value = descriptor.GetValue(rawControl);
            }

            return(new TargetProperty {
                Descriptor = descriptor, OwningControlRaw = rawControl, OwningControl = control, Value = value
            });
        }
        protected override void OnInit(EventArgs e)
        {
            IBindingTarget page = this.Page as IBindingTarget;

            if (page == null)
            {
                throw new InvalidOperationException("This control can only be used on pages that implement IBindingTarget");
            }

            WebformsControlService controlService = new WebformsControlService();

            IBindingTarget ctrl = controlService.FindControlRecursive(page, this.GetType());

            if (ctrl != null && ctrl.UniqueID != this.UniqueID)
            {
                throw new InvalidOperationException("Only one control of type BindingOptions can appear of each page");
            }

            base.OnInit(e);
        }
        private void ExecuteCommandBind(IBindingTarget control, BindingDef binding)
        {
            //Get the ICommandObject from the source
            SourceProperty commandInstance = binding.SourceExpression.ResolveAsSource(this.DataContext);
            object         commandObject   = commandInstance.Value;
            ICommand       iCommand        = commandObject as ICommand;

            //Get the property setter for the event to which we will bind
            TargetEvent evt = binding.ResolveAsTargetEvent(this.bindingContainer);

            //bind
            Type     eventDelegateType = evt.Descriptor.EventHandlerType;
            Delegate handler           = new EventHandler((s, e) => iCommand.Execute(this));

            evt.Descriptor.GetAddMethod().Invoke(evt.OwningControl, new object[] { handler });

            control.Visible = iCommand.CanExecute(this);

            //listen to canExecuteChanged
            iCommand.CanExecuteChanged += new EventHandler((s, e) => control.Visible = iCommand.CanExecute(this));
        }
        public IBindingTarget FindControlRecursive(IBindingTarget root, Type type)
        {
            Control resolvedRoot = Resolve(root);

            if (resolvedRoot.GetType() == type)
            {
                return(root);
            }

            foreach (Control ctl in this.GetChildren(root))
            {
                IBindingTarget webFormControl = new WebformControl(ctl);
                IBindingTarget foundCtl       = FindControlRecursive(webFormControl, type);
                if (foundCtl != null)
                {
                    return(foundCtl);
                }
            }

            return(null);
        }
        public object Unwrap(IBindingTarget bindingTarget)
        {
            if (bindingTarget == null)
            {
                throw new ArgumentException("Cannot unwrap null object", "bindingTarget");
            }

            WebformControl webControl = bindingTarget as WebformControl;

            if (webControl == null)
            {
                throw new ArgumentException(string.Format("Unable to unwrap objects of type {0}", bindingTarget.GetType().FullName), "bindingTarget");
            }

            if (webControl.Control == null)
            {
                throw new ArgumentException("Unwrapped object is null", "bindingTarget");
            }

            return(webControl.Control);
        }
        /// <summary>
        /// Execute a bind in the context of a collection container.
        /// </summary>
        /// <remarks>
        /// Specficy the target control directly.
        /// </remarks>
        /// <param name="control">The target control</param>
        /// <param name="collectionItemContainer">
        /// The collection item container. E.g. In the case of a repeater a RepeaterItem.
        /// This is available from the ItemTemplate control via (IDataItemContainer)container.NamingContainer.
        /// </param>
        ///<param name="targetPropertyName">The property on the targetcontrol.</param>
        /// <param name="bindingOptions"></param>
        /// <returns>The result (value) of the bind</returns>
        public object ExecuteBind(IBindingTarget control, IBindingTarget <IDataItemContainer> collectionItemContainer, Options bindingOptions, string targetPropertyName)
        {
            if (control == null || bindingContainer == null || bindingOptions == null || string.IsNullOrEmpty(bindingOptions.Path))
            {
                return(null);
            }

            object     dataValue = null;
            BindingDef binding   = null;

            IBindingTarget genericContainer = null;
            object         baseContext      = this.DataContext;

            if (bindingOptions.PathMode == PathMode.Relative && collectionItemContainer != null)
            {
                IDataItemContainer containerControl = collectionItemContainer.TargetInstance;
                dataValue = DataBinder.Eval(containerControl.DataItem, bindingOptions.Path);
                bool isCollectionBinding = DetermineIfCollection(control, dataValue);
                binding          = new BindingDef(control, targetPropertyName, containerControl.DataItemIndex, DataStorage, bindingOptions, isCollectionBinding, controlService);
                genericContainer = collectionItemContainer;
            }
            else
            {
                dataValue = DataBinder.Eval(baseContext, bindingOptions.Path);
                bool isCollectionBinding = DetermineIfCollection(control, dataValue);
                binding          = new BindingDef(control, targetPropertyName, bindingOptions, isCollectionBinding, controlService);
                genericContainer = controlService.GetParent(control);
            }

            //determine binding direction is not explicitly set based on the value and contorl
            if (bindingOptions.Mode == BindingMode.Unset)
            {
                bindingOptions.Mode = DetermineBindingDirection(control, dataValue);
            }

            //store the binding
            DataStorage.Add(binding);

            return(AttemptConvert(binding, dataValue, genericContainer));
        }
Example #31
0
        /// <summary>
        /// Sets the current target.
        /// </summary>
        /// <remarks>
        /// This will clear current hierarchy and regenerate a new one.
        /// </remarks>
        /// <param name="target">The target to set.</param>
        /// <typeparam name="T">The type of the target.</typeparam>
        public void SetTarget <T>(T target)
        {
            if (TryGetTarget(out T current) && EqualityComparer <T> .Default.Equals(target, current))
            {
                // Nothing to do..
                return;
            }

            if (null != m_BindingTarget)
            {
                var declaredType = typeof(T);

                var targetType = declaredType;
                if (typeof(T).IsClass && EqualityComparer <T> .Default.Equals(target, default))
                {
                    targetType = target.GetType();
                }

                if (declaredType == m_BindingTarget.DeclaredType && targetType == m_BindingTarget.TargetType)
                {
                    ((BindingTarget <T>)m_BindingTarget).Target = target;
                    return;
                }
            }

            m_BindingTarget?.Release();

            if (RuntimeTypeInfoCache <T> .CanBeNull && EqualityComparer <T> .Default.Equals(target, default))
            {
                m_BindingTarget = null;
                return;
            }

            m_BindingTarget = new BindingTarget <T>(this, target);
            name            = TypeUtility.GetTypeDisplayName(target.GetType());
            m_BindingTarget.GenerateHierarchy();
        }
Example #32
0
 public static Binding GetOrCreateBinding(Gtk.Widget widget, Object source, IBindingTarget target, BindingInfo info)
 {
     Binding result = null;
     if (!mBindings.TryGetValue(target, out result)) {
         result = new Binding();
         result.Info = info;
         result.Connect(widget, source, target);
         mBindings[target] = result;
     }
     return result;
 }