Example #1
0
        /// <summary>
        /// Adds binding based on convention, Uses the name of bound property in VM as key in IDataContainer
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"><see cref="MemberExpression"/> that gets CLR property </param>
        /// <param name="updateSourceOnPropertyChange">Whether or not to update value inside <see cref="DataContainer"/> when Target value changes</param>
        public bool SetBinding <T>(Expression <Func <T> > expression, BindingMode mode = BindingMode.TwoWay)
        {
            MemberExpression memberExpression = expression.Body as MemberExpression;
            var propinfo = memberExpression.Member as PropertyInfo;

            if (memberExpression is null)
            {
                throw new InvalidOperationException("Body of Lambda expression must be a Member expression");
            }

            var property = this.FindRecursive(propinfo.Name);

            if (property is null)
            {
                DataContainerEvents.NotifyError($"Binding failed, key : {propinfo.Name} not found");

                return(false);
            }

            var target = Expression.Lambda(memberExpression.Expression).Compile().DynamicInvoke();

            if (BindingManager.Instance.GetBinding(target, propinfo.Name) is null)
            {
                var binding = new DataObjectBinding(target, property, propinfo, mode);
                if (mode != BindingMode.OneTime)
                {
                    BindingManager.Instance.AddBinding(binding);
                }
            }

            return(true);
        }
 internal void RemoveBinding(DataObjectBinding pb)
 {
     pb.Dispose();
     _bindings.Remove(pb);
 }
 internal void AddBinding(DataObjectBinding pb) => _bindings.Add(pb);