Exemplo n.º 1
0
        /// <summary>
        /// Evaluates the specified expression in the context of specified control.
        /// </summary>
        public object Evaluate(ValueBindingExpression expression, DotvvmProperty property, DotvvmBindableControl contextControl)
        {
            var visitor = EvaluateDataContextPath(contextControl);

            // evaluate the final expression
            EvaluateBinding(visitor, expression.Expression, expression.GetViewModelPathExpression(contextControl, property));
            return visitor.Result;
        }
Exemplo n.º 2
0
        public void ThisBinding()
        {
            var dataContext = DataContextStack.Create(typeof(string));

            var binding1 = ValueBindingExpression.CreateThisBinding <string>(service, dataContext);
            var binding2 = ValueBindingExpression.CreateThisBinding <string>(service, dataContext);
            var binding3 = ValueBindingExpression.CreateThisBinding <object>(service, dataContext);
            var binding4 = ValueBindingExpression.CreateThisBinding <string>(service, DataContextStack.Create(typeof(string), dataContext));
            var binding5 = ValueBindingExpression.CreateThisBinding <string>(service, DataContextStack.Create(typeof(string), dataContext));

            Assert.AreEqual(binding1, binding2);
            Assert.AreNotEqual(binding1, binding3);
            Assert.AreNotEqual(binding1, binding4);
            Assert.AreNotEqual(binding2, binding3);
            Assert.AreNotEqual(binding2, binding4);
            Assert.AreNotEqual(binding3, binding4);
            Assert.AreEqual(binding4, binding5);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Evaluates the PropertyInfo for the expression.
        /// </summary>
        public PropertyInfo EvaluateProperty(ValueBindingExpression expression, DotvvmProperty property, DotvvmBindableControl control, out object target)
        {
            var visitor = EvaluateDataContextPath(control);
            target = visitor.Result;

            // evaluate the final expression
            var node = ParseBinding(expression.Expression);
            string propertyName = null;
            if (node is IdentifierNameSyntax)
            {
                propertyName = ((IdentifierNameSyntax)node).ToString();
            }
            else if (node is MemberAccessExpressionSyntax)
            {
                target = visitor.Visit(((MemberAccessExpressionSyntax)node).Expression);
                propertyName = ((MemberAccessExpressionSyntax)node).Name.ToString();
            }

            if (propertyName != null && !visitor.IsSpecialPropertyName(propertyName))
            {
                var propertyInfo = target.GetType().GetProperty(propertyName);
                if (propertyInfo != null)
                {
                    return propertyInfo;
                }
            }
            throw new NotSupportedException(string.Format("Cannot update the source of the binding '{0}'!", expression.Expression));
        }