public object GetValue(RedwoodProperty property, bool ignoreInheritance = false) { VerifyAccess(); ValueEntry entry = GetValueDirect(property, true, ignoreInheritance); return entry.Value; }
public BindingExpression(BindingMode mode, Binding.Parsing.Expressions.BindingPathExpression path, RedwoodProperty sourceProperty = null, RedwoodBindable source = null) { Path = path; Mode = mode; SourceProperty = sourceProperty ?? Controls.RedwoodControl.DataContextProperty; Source = source; }
public void SetValue(RedwoodProperty property, object value) { if (value == RedwoodProperty.UnsetValue) { ClearValue(property); return; } VerifyAccess(); property.ValidatePropertyValue(value); var itemRef = TryFindLocalValue(property.Id); UpdateLocalValue(itemRef, new ValueEntry() { PropertyId = property.Id, Value = value }); // set parent if property is inheritance source if (property.Metadata.IsInheritanceSource) { if (value is RedwoodBindable) ((RedwoodBindable)value).SetParent(this); if(value is IEnumerable) foreach (var item in (IEnumerable)value) if(item is RedwoodBindable) ((RedwoodBindable)item).SetParent(this); } }
public RedwoodPropertyAccessor(RedwoodProperty propInfo) { if (propInfo == null) throw new ArgumentNullException("propInfo"); PropertyInfo = propInfo; }
private void AddEventAttributeToRender(IHtmlWriter writer, RenderContext context, string name, RedwoodProperty property) { var binding = GetCommandBinding(property); if (binding != null) { writer.AddAttribute(name, KnockoutHelper.GenerateClientPostBackScript(binding, context, this)); } }
public void ClearValue(RedwoodProperty property) { VerifyAccess(); var itemRef = TryFindLocalValue(property.Id); ClearLocalValue(itemRef); }
/// <summary> /// Determines whether it is legal to invoke a command on the specified property. /// </summary> public bool ValidateCommand(RedwoodProperty targetProperty) { if (targetProperty == ClickProperty) { return Enabled && Visible; } return false; }
/// <summary> /// Gets the view model path expression. /// </summary> public override string GetViewModelPathExpression(RedwoodBindableControl control, RedwoodProperty property) { // find the parent markup control and calculate number of DataContext changes int numberOfDataContextChanges; var current = control.GetClosestControlBindingTarget(out numberOfDataContextChanges) as RedwoodBindableControl; current.EnsureControlHasId(); return string.Join(".", Enumerable.Range(0, numberOfDataContextChanges).Select(i => "_parent").Concat(new[] { "_controlState_" + current.ID, Expression })); }
public static CommandMarkupExpression GetCommandExpressionOrNull(RedwoodProperty property, RedwoodBindable obj) { var value = obj.GetRawValue(property); if (value is CommandMarkupExpression) { return (CommandMarkupExpression)value; } return null; }
public static BindingMarkup GetBindingExpressionOrNull(RedwoodProperty property, RedwoodBindable obj) { var value = obj.GetRawValue(property); if (value is BindingMarkup) { return (BindingMarkup)value; } return null; }
/// <summary> /// Translates to knockout property. /// </summary> public static string TranslateToKnockoutProperty(RedwoodBindable target, RedwoodProperty property, BindingMarkup binding) { var path = binding.Path; // TODO: support for other than two-way modes var sb = new StringBuilder(); var result = TranslateToKnockoutProperty(path, sb, allowConstants: false); if (result != null) { ThrowBindingContainsUnsupportedExpression(); } return sb.ToString(); }
public void RegisterProperty(RedwoodProperty property) { var key = new NameKey(property.Name, property.OwnerType); propertiesLock.EnterWriteLock(); try { if (propertiesDict.ContainsKey(key)) throw new InvalidOperationException(string.Format("Redwood property {0} already exists on {1}.", property.Name, property.OwnerType.FullName)); propertiesDict.Add(key, property); } finally { propertiesLock.ExitWriteLock(); } }
/// <summary> /// Evaluates the specified expression. /// </summary> public override object Evaluate(RedwoodBindableControl control, RedwoodProperty property) { ValidateExpression(Expression); // find the parent markup control and calculate number of DataContext changes int numberOfDataContextChanges; var current = control.GetClosestControlBindingTarget(out numberOfDataContextChanges) as RedwoodBindableControl; if (current == null || !current.RequiresControlState) { throw new Exception("The {controlState: ...} binding can only be used in a markup control that supports ControlState!"); // TODO: exception handling } else { object value; return current.ControlState.TryGetValue(Expression, out value) ? value : DefaultValue; } }
/// <summary> /// Evaluates the binding. /// </summary> public override object Evaluate(Controls.RedwoodBindableControl control, RedwoodProperty property) { if (!Expression.Contains(".")) { throw new Exception("Invalid resource name! Use Namespace.ResourceType.ResourceKey!"); } // parse expression var lastDotPosition = Expression.LastIndexOf("."); var resourceType = Expression.Substring(0, lastDotPosition); var resourceKey = Expression.Substring(lastDotPosition + 1); // find the resource manager var resourceManager = cachedResourceManagers.GetOrAdd(resourceType, GetResourceManager); // return the value return resourceManager.GetString(resourceKey); }
/// <summary> /// Finds the binding of the specified type on the specified viewmodel path. /// </summary> private CommandBindingExpression FindCommandBinding(string[] path, string command, RedwoodControl viewRootControl, ref string validationTargetPath, out RedwoodBindableControl targetControl, out RedwoodProperty targetProperty) { // walk the control tree and find the path CommandBindingExpression result = null; RedwoodBindableControl resultControl = null; RedwoodProperty resultProperty = null; string resultValidationTargetPath = validationTargetPath; var walker = new NonEvaluatingControlTreeWalker(viewRootControl); walker.ProcessControlTree((ViewModel, control) => { // compare path if (result == null && control is RedwoodBindableControl && ViewModelPathComparer.AreEqual(path, walker.CurrentPathArray)) { // find bindings of current control var bindableControl = (RedwoodBindableControl)control; var binding = bindableControl.GetAllBindings().Where(p => p.Value is CommandBindingExpression) .FirstOrDefault(b => b.Value.Expression == command); if (binding.Key != null) { // we have found the binding, now get the validation path var currentValidationTargetPath = KnockoutHelper.GetValidationTargetExpression(bindableControl, true); if (currentValidationTargetPath == resultValidationTargetPath) { // the validation path is equal, we have found the binding result = (CommandBindingExpression)binding.Value; resultControl = bindableControl; resultProperty = binding.Key; resultValidationTargetPath = KnockoutHelper.GetValidationTargetExpression(bindableControl, false); } } } }); validationTargetPath = resultValidationTargetPath; targetControl = resultControl; targetProperty = resultProperty; return result; }
public override object GetValue(RedwoodBindable target, RedwoodProperty property) { var dataContext = GetDataContext(Source ?? target, property); var result = Path.Evaluate(dataContext); return result; }
public override string TranslateToClientScript(Controls.RedwoodBindableControl control, RedwoodProperty property) { throw new NotSupportedException(); }
/// <summary> /// Translates the expression to client script. /// </summary> public override string TranslateToClientScript(RedwoodBindableControl control, RedwoodProperty property) { ValidateExpression(Expression); // find the parent markup control and calculate number of DataContext changes int numberOfDataContextChanges; var current = control.GetClosestControlBindingTarget(out numberOfDataContextChanges) as RedwoodBindableControl; current.EnsureControlHasId(); return string.Join(".", Enumerable.Range(0, numberOfDataContextChanges).Select(i => "$parent").Concat(new[] { "$controlState()", current.ID + "()", Expression })); }
private object GetDataContext(RedwoodBindable target, RedwoodProperty property) { object result = null; bool isDataContextBinding = (property == SourceProperty); if (isDataContextBinding) { // if this is binding on "DataContext" - read value from parent if (target.Parent != null) result = target.Parent.GetRawValue(SourceProperty); } else { result = target.GetValue(SourceProperty); } return result; }
/// <summary> /// Evaluates the binding. /// </summary> public abstract object Evaluate(RedwoodBindableControl control, RedwoodProperty property);
/// <summary> /// Gets the view model path expression. /// </summary> public virtual string GetViewModelPathExpression(RedwoodBindableControl control, RedwoodProperty property) { return Expression; }
/// <summary> /// Evaluates the binding. /// </summary> public override object Evaluate(RedwoodBindableControl control, RedwoodProperty property) { // TODO: implement server evaluation throw new NotImplementedException(); }
/// <summary> /// Translates the binding expression to the knockout command name. /// </summary> public static string TranslateToKnockoutCommand(RedwoodControl target, RedwoodProperty property, CommandMarkupExpression binding) { return DataContextPathBuilder.Default.BuildPath(target); }
/// <summary> /// Translates the binding to client script. /// </summary> /// <param name="control"></param> /// <param name="property"></param> public override string TranslateToClientScript(RedwoodBindableControl control, RedwoodProperty property) { return translator.Translate(Expression); }
private ValueEntry GetValueDirect(RedwoodProperty property, bool resolveExpressions, bool ignoreInheritance) { var itemRef = TryFindLocalValue(property.Id); if (itemRef.Found) { // local value var localValue = GetLocalEntry(itemRef); if (localValue.Value is BindingExpression && resolveExpressions) { // evaluate expression var localValueExpression = (BindingExpression)localValue.Value; localValue.Value = localValueExpression.GetValue(this, property); } return localValue; } if (property.Metadata.IsInherited && parent != null && !ignoreInheritance) { // inherited value return parent.GetValueDirect(property, resolveExpressions, ignoreInheritance); } // default value return new ValueEntry() { PropertyId = property.Id, Value = property.Metadata.DefaultValue }; }
/// <summary> /// Evaluates the binding. /// </summary> public override object Evaluate(RedwoodBindableControl control, RedwoodProperty property) { return evaluator.Evaluate(this, property, control); }
/// <summary> /// Updates the viewModel with the new value. /// </summary> public virtual void UpdateSource(object value, RedwoodBindableControl control, RedwoodProperty property) { object target; var propertyInfo = evaluator.EvaluateProperty(this, property, control, out target); propertyInfo.SetValue(target, ReflectionUtils.ConvertValue(value, propertyInfo.PropertyType)); }
/// <summary> /// Updates the value. /// </summary> public override void UpdateSource(object value, RedwoodBindableControl control, RedwoodProperty property) { control.ControlState[property.Name] = value; }
public static RedwoodBindableControl WithBinding(this RedwoodBindableControl control, RedwoodProperty property, BindingExpression expression) { control.SetBinding(property, expression); return control; }
/// <summary> /// Translates the binding to client script. /// </summary> /// <param name="control"></param> /// <param name="property"></param> public abstract string TranslateToClientScript(RedwoodBindableControl control, RedwoodProperty property);