public BindingManager <TViewModel> Bind <T>(Expression <Func <TViewModel, T> > sourceExpr, Action <T> setterDelegate)
        {
            if (!(sourceExpr.Body is MemberExpression member))
            {
                throw new ArgumentException($"Expression '{sourceExpr.Name}' refers to a method, not a property.");
            }

            var propInfo = member.Member as PropertyInfo;

            if (propInfo == null)
            {
                throw new ArgumentException($"Expression '{sourceExpr.Name}' refers to a field, not a property.");
            }

            if (propInfo.ReflectedType == null ||
                typeof(TViewModel) != propInfo.ReflectedType &&
                !typeof(TViewModel).IsSubclassOf(propInfo.ReflectedType))
            {
                throw new ArgumentException(
                          $"Expresion '{sourceExpr}' refers to a property that is not from type {typeof(TViewModel).Name}.");
            }

            var sourceGetter = sourceExpr.Compile();

            PropertyBindings.Add(propInfo.Name, new PropertyChangedHandler <TViewModel>(SynchronizationContext.Current,
                                                                                        vm =>
            {
                var val = sourceGetter.Invoke(vm);
                setterDelegate.Invoke(val);
            }));

            return(this);
        }
Beispiel #2
0
 internal AjaxBase(System.Web.UI.Control control, HttpContext context)
 {
     if (!(control is IAjaxControl))
     {
         throw new System.InvalidOperationException();
     }
     _control    = control;
     _properties = WebBindable.Properties(_control.GetType());
     _context    = context;
     _bindings   = new PropertyBindings((IAjaxControl)_control);
 }
Beispiel #3
0
        /// <summary>
        /// String of name=value pairs representing server-side parameters
        /// </summary>
        /// <param name="properties">array of bindable control properties</param>
        /// <param name="bindings">list of DOM property bindings</param>
        static internal string[] PropertyLists(PropertyInfo[] properties,
                                               PropertyBindings bindings, IAjaxControl control)
        {
            // bound dynamic parameters
            StringBuilder boundList = new StringBuilder();
            // static parameters
            StringBuilder staticList = new StringBuilder();
            string        pair;
            Regex         empty         = new Regex("^0|null|\\[\\]$");
            Type          attributeType = typeof(WebBindable);

            foreach (PropertyInfo p in properties)
            {
                if (p.CanRead)
                {
                    pair = string.Format("\"{0}={{0}}\",", p.Name);
                    if (bindings.Contains(p))
                    {
                        boundList.AppendFormat(pair, bindings[p]);
                    }
                    else
                    {
                        string value = p.GetValue(control, null).ToJSON();

                        if (!string.IsNullOrEmpty(value) && !empty.IsMatch(value))
                        {
                            value = value.Trim('"');
                        }
                        else if (WebBindable.AlwaysBind(p))
                        {
                            // always bindable properties are scripted even if null
                            value = EcmaScript.Null;
                        }
                        else
                        {
                            // ignore this property
                            continue;
                        }
                        staticList.AppendFormat(pair, value);
                    }
                }
            }
            if (boundList.Length > 2)
            {
                boundList.Length -= 1;
            }                                                                   // remove trailing comma
            staticList.AppendFormat("\"id={0}\"", control.ID);

            return(new string[] { staticList.ToString(), boundList.ToString() });
        }
        private void ViewModelOnPropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
        {
            if (string.IsNullOrWhiteSpace(eventArgs.PropertyName))
            {
                return;
            }
            if (!PropertyBindings.ContainsKey(eventArgs.PropertyName))
            {
                return;
            }

            var handler = PropertyBindings[eventArgs.PropertyName];

            handler.Execute((TViewModel)sender);
        }
Beispiel #5
0
 public ActivityInfo AddPropertyBinding([NotNull] PropertyBindingInfo binding)
 {
     PropertyBindings.Add(binding.NotNull());
     return(this);
 }