public static CommandBindingExpression GetExtensionCommand(this DotvvmControl control, string methodUsageId) { var propertyName = control.GetType().FullName + "/" + methodUsageId; var property = DotvvmProperty.ResolveProperty(typeof(PropertyBox), propertyName); return(control.GetCommandBinding(property) as CommandBindingExpression); }
public static DotvvmProperty Register(string propertyName, Type propertyType, Type declaringType, object?defaultValue, bool isValueInherited, DotvvmProperty?property, ICustomAttributeProvider attributeProvider, bool throwOnDuplicitRegistration = true) { var fullName = declaringType.FullName + "." + propertyName; if (property == null) { property = new DotvvmProperty(); } property.Name = propertyName; property.IsValueInherited = isValueInherited; property.DeclaringType = declaringType; property.PropertyType = propertyType; property.DefaultValue = defaultValue; InitializeProperty(property, attributeProvider); if (!registeredProperties.TryAdd(fullName, property)) { if (throwOnDuplicitRegistration) { throw new ArgumentException($"Property is already registered: {fullName}"); } else { property = registeredProperties[fullName]; } } return(property); }
/// <summary> /// Registers the specified DotVVM property. /// </summary> public static DotvvmPropertyWithFallback Register <TPropertyType, TDeclaringType>(string propertyName, DotvvmProperty defaultProperty, bool defaultPropertyInherit, bool isValueInherited = false) { var property = new DotvvmPropertyWithFallback() { DefaultProperty = defaultProperty, DefaultPropertyInherit = defaultPropertyInherit }; return(DotvvmProperty.Register <TPropertyType, TDeclaringType>(propertyName, isValueInherited: isValueInherited, property: property) as DotvvmPropertyWithFallback); }
public static CommandBindingExpression RegisterExtensionCommand(this DotvvmControl control, Delegate action, string methodUsageId) { var id = control.GetDotvvmUniqueId() + methodUsageId; var propertyName = control.GetType().FullName + "/" + methodUsageId; var property = DotvvmProperty.Register <object, ExtensionCommands>(propertyName); var binding = new CommandBindingExpression(action, id); control.SetBinding(property, binding); return(binding); }
public static CommandBindingExpression?GetExtensionCommand(this DotvvmControl control, string methodUsageId) { var propertyName = control.GetType().FullName + "/" + methodUsageId; var property = DotvvmProperty.ResolveProperty(typeof(PropertyBox), propertyName); if (property is null) { throw new Exception($"Extension command {propertyName} has not been registered."); } return(control.GetCommandBinding(property) as CommandBindingExpression); }
/// <summary> /// Registers the specified DotVVM property. /// </summary> public static DotvvmProperty Register <TPropertyType, TDeclaringType>(string propertyName, TPropertyType defaultValue = default(TPropertyType), bool isValueInherited = false, DotvvmProperty property = null) { var fullName = typeof(TDeclaringType).FullName + "." + propertyName; var field = typeof(TDeclaringType).GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (field == null) { throw new ArgumentException($"'{typeof(TDeclaringType).Name}' does not contain static field '{propertyName}Property'."); } if (registeredProperties.ContainsKey(fullName)) { throw new ArgumentException($"Property is already registered: {fullName}"); } return(registeredProperties.GetOrAdd(fullName, _ => { var propertyInfo = typeof(TDeclaringType).GetProperty(propertyName); var markupOptions = propertyInfo?.GetCustomAttribute <MarkupOptionsAttribute>() ?? field?.GetCustomAttribute <MarkupOptionsAttribute>() ?? new MarkupOptionsAttribute() { AllowBinding = true, AllowHardCodedValue = true, MappingMode = MappingMode.Attribute, Name = propertyName }; if (string.IsNullOrEmpty(markupOptions.Name)) { markupOptions.Name = propertyName; } if (property == null) { property = new DotvvmProperty(); } property.Name = propertyName; property.DefaultValue = defaultValue; property.DeclaringType = typeof(TDeclaringType); property.PropertyType = typeof(TPropertyType); property.IsValueInherited = isValueInherited; property.PropertyInfo = propertyInfo; property.DataContextChangeAttributes = (propertyInfo != null ? propertyInfo.GetCustomAttributes <DataContextChangeAttribute>(true) : field.GetCustomAttributes <DataContextChangeAttribute>()).ToArray(); property.DataContextManipulationAttribute = propertyInfo != null ? propertyInfo.GetCustomAttribute <DataContextStackManipulationAttribute>(true) : field.GetCustomAttribute <DataContextStackManipulationAttribute>(); if (property.DataContextManipulationAttribute != null && property.DataContextChangeAttributes.Any()) { throw new ArgumentException($"{nameof(DataContextChangeAttributes)} and {nameof(DataContextManipulationAttribute)} can not be set both at property '{fullName}'."); } property.MarkupOptions = markupOptions; property.IsBindingProperty = typeof(IBinding).IsAssignableFrom(property.PropertyType); return property; })); }
public static CommandBindingExpression RegisterExtensionCommand(this DotvvmControl control, Delegate action, string methodUsageId) { var bindingService = control.GetValue(Internal.RequestContextProperty).CastTo <IDotvvmRequestContext>() .Configuration.ServiceProvider.GetRequiredService <BindingCompilationService>(); var id = control.GetDotvvmUniqueId() + methodUsageId; var propertyName = control.GetType().FullName + "/" + methodUsageId; var property = DotvvmProperty.ResolveProperty(typeof(PropertyBox), propertyName) ?? DotvvmProperty.Register(propertyName, typeof(object), typeof(PropertyBox), null, false, null, typeof(PropertyBox), throwOnDuplicitRegistration: false); var binding = new CommandBindingExpression(bindingService, action, id); control.SetBinding(property, binding); return(binding); }
public static CommandBindingExpression RegisterExtensionCommand(this DotvvmControl control, Delegate action, string methodUsageId) { var bindingService = control.GetValue(Internal.RequestContextProperty).CastTo <IDotvvmRequestContext>() .Configuration.ServiceLocator.GetService <BindingCompilationService>(); var id = control.GetDotvvmUniqueId() + methodUsageId; var propertyName = control.GetType().FullName + "/" + methodUsageId; var property = DotvvmProperty.Register <object, PropertyBox>(propertyName); var binding = new CommandBindingExpression(bindingService, action, id); control.SetBinding(property, binding); return(binding); }
/// <summary> /// Evaluates the binding. /// </summary> public object Evaluate(DotvvmBindableObject control, DotvvmProperty property, params object[] args) { var action = GetCommandDelegate(control, property); if (action is Command) { return((action as Command)()); } if (action is Action) { (action as Action)(); return(null); } return(action.DynamicInvoke(args)); }
public static void AndAssignProperty(this DotvvmBindableObject obj, DotvvmProperty property, object value) { if (property.PropertyType != typeof(bool)) { throw new NotSupportedException($"Can only AND boolean properties, {property} is of type {property.PropertyType}"); } if (!obj.IsPropertySet(property)) { obj.SetValue(property, value); } else { if (value is bool b && !b) { obj.SetValue(property, false); }
public override Type GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, ResolvedControl control, DotvvmProperty dproperty = null) { var property = DotvvmProperty.ResolveProperty(control.Metadata.Type, PropertyName); ResolvedPropertySetter propertyValue; if (control.Properties.TryGetValue(property, out propertyValue)) { var binding = propertyValue as ResolvedPropertyBinding; if (binding == null) { return(dataContext); } return(binding.Binding.GetExpression().Type); } else { return(dataContext); } }
/// <summary> /// Finds the binding of the specified type on the specified viewmodel path. /// </summary> private FindBindingResult FindControlCommandBinding(string[] path, string commandId, DotvvmControl viewRootControl, DotvvmControl targetControl, string validationTargetPath) { // walk the control tree and find the path ControlCommandBindingExpression resultBinding = null; DotvvmProperty resultProperty = null; DotvvmControl resultControl = null; var walker = new ControlTreeWalker(viewRootControl); walker.ProcessControlTree((control) => { // compare path if (ViewModelPathComparer.AreEqual(path, walker.CurrentPathArray)) { // find bindings of current control var binding = control.GetAllBindings().Where(p => p.Value is ControlCommandBindingExpression) .FirstOrDefault(b => b.Value.BindingId == commandId); if (binding.Key != null) { // verify that the target control is the control command target if (control.GetClosestControlBindingTarget() == targetControl) { // we have found the binding, now get the validation path var currentValidationTargetPath = KnockoutHelper.GetValidationTargetExpression(control); if (currentValidationTargetPath == validationTargetPath) { // the validation path is equal, we have found the binding resultBinding = (ControlCommandBindingExpression)binding.Value; resultProperty = binding.Key; resultControl = control; } } } } }); return(new FindBindingResult { Property = resultProperty, Binding = resultBinding, Control = resultControl }); }
public static GroupedDotvvmProperty Create(DotvvmPropertyGroup group, string name) { var propname = group.Name + ":" + name; var prop = new GroupedDotvvmProperty(name, group) { PropertyType = group.PropertyType, DeclaringType = group.DeclaringType, DefaultValue = group.DefaultValue, IsValueInherited = false, Name = propname }; if (group.PropertyGroupMode == PropertyGroupMode.ValueCollection) { prop.IsVirtual = true; } DotvvmProperty.InitializeProperty(prop, (MemberInfo)group.DescriptorField ?? group.PropertyInfo); return(prop); }
/// <summary> /// Registers the specified DotVVM property. /// </summary> public static DotvvmProperty Register <TPropertyType, TDeclaringType>(string propertyName, TPropertyType defaultValue = default(TPropertyType), bool isValueInherited = false, DotvvmProperty property = null) { var fullName = typeof(TDeclaringType).FullName + "." + propertyName; var field = typeof(TDeclaringType).GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public); return(registeredProperties.GetOrAdd(fullName, _ => { var propertyInfo = typeof(TDeclaringType).GetProperty(propertyName); var markupOptions = propertyInfo?.GetCustomAttribute <MarkupOptionsAttribute>() ?? field?.GetCustomAttribute <MarkupOptionsAttribute>() ?? new MarkupOptionsAttribute() { AllowBinding = true, AllowHardCodedValue = true, MappingMode = MappingMode.Attribute, Name = propertyName }; if (string.IsNullOrEmpty(markupOptions.Name)) { markupOptions.Name = propertyName; } if (property == null) { property = new DotvvmProperty(); } property.Name = propertyName; property.DefaultValue = defaultValue; property.DeclaringType = typeof(TDeclaringType); property.PropertyType = typeof(TPropertyType); property.IsValueInherited = isValueInherited; property.PropertyInfo = propertyInfo; property.MarkupOptions = markupOptions; property.IsBindingProperty = typeof(IBinding).IsAssignableFrom(property.PropertyType); return property; })); }
public static void InitializeProperty(DotvvmProperty property, ICustomAttributeProvider attributeProvider) { var propertyInfo = property.DeclaringType.GetProperty(property.Name); var markupOptions = propertyInfo?.GetCustomAttribute <MarkupOptionsAttribute>() ?? attributeProvider.GetCustomAttribute <MarkupOptionsAttribute>() ?? new MarkupOptionsAttribute() { AllowBinding = true, AllowHardCodedValue = true, MappingMode = MappingMode.Attribute, Name = property.Name }; if (string.IsNullOrEmpty(markupOptions.Name)) { markupOptions.Name = property.Name; } if (property == null) { property = new DotvvmProperty(); } property.PropertyInfo = propertyInfo; property.DataContextChangeAttributes = (propertyInfo != null ? propertyInfo.GetCustomAttributes <DataContextChangeAttribute>(true) : attributeProvider.GetCustomAttributes <DataContextChangeAttribute>()).ToArray(); property.DataContextManipulationAttribute = propertyInfo != null? propertyInfo.GetCustomAttribute <DataContextStackManipulationAttribute>(true) : attributeProvider.GetCustomAttribute <DataContextStackManipulationAttribute>(); if (property.DataContextManipulationAttribute != null && property.DataContextChangeAttributes.Any()) { throw new ArgumentException($"{nameof(DataContextChangeAttributes)} and {nameof(DataContextManipulationAttribute)} can not be set both at property '{property.FullName}'."); } property.MarkupOptions = markupOptions; property.IsBindingProperty = typeof(IBinding).IsAssignableFrom(property.PropertyType); }
/// <summary> /// Evaluates the binding. /// </summary> public object Evaluate(Controls.DotvvmBindableObject control, DotvvmProperty property) { if (Delegate != null) { return(ExecDelegate(control, true)); } if (!OriginalString.Contains(".")) { throw new Exception("Invalid resource name! Use Namespace.ResourceType.ResourceKey!"); } // parse expression var expressionText = OriginalString.Trim(); var lastDotPosition = expressionText.LastIndexOf(".", StringComparison.Ordinal); var resourceType = expressionText.Substring(0, lastDotPosition); var resourceKey = expressionText.Substring(lastDotPosition + 1); // find the resource manager var resourceManager = cachedResourceManagers.GetOrAdd(resourceType, GetResourceManager); // return the value return(resourceManager.GetString(resourceKey)); }
public override Type GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, ResolvedControl control, DotvvmProperty property = null) { return(type); }
public override Type GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, DotvvmBindableObject control, DotvvmProperty property = null) { return(Type); }
public static DelegateActionProperty <TValue> Register <TDeclaringType>(string name, Action <IHtmlWriter, IDotvvmRequestContext, DotvvmProperty, DotvvmControl> func, TValue defaultValue = default(TValue)) { return((DelegateActionProperty <TValue>)DotvvmProperty.Register <TValue, TDeclaringType>(name, defaultValue, false, new DelegateActionProperty <TValue>(func))); }
public static DotvvmProperty Register <TPropertyType, TDeclaringType>(Expression <Func <TDeclaringType, object> > propertyAccessor, DotvvmProperty fallbackProperty, bool isValueInherited = false) { var property = ReflectionUtils.GetMemberFromExpression(propertyAccessor.Body) as PropertyInfo; if (property == null) { throw new ArgumentException("The expression should be simple property access", nameof(propertyAccessor)); } return(Register <TPropertyType, TDeclaringType>(property.Name, fallbackProperty, isValueInherited)); }
public DotvvmPropertyWithFallback(DotvvmProperty fallbackProperty) { this.FallbackProperty = fallbackProperty; }
public override Type GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, ResolvedControl control, DotvvmProperty property = null) { return(ReflectionUtils.GetEnumerableType(dataContext)); }
public static DotvvmPropertyWithFallback Register <TPropertyType, TDeclaringType>(string propertyName, DotvvmProperty fallbackProperty, bool isValueInherited = false) { var property = new DotvvmPropertyWithFallback(fallbackProperty); return((DotvvmPropertyWithFallback)Register <TPropertyType, TDeclaringType>(propertyName, isValueInherited: isValueInherited, property: property)); }
/// <summary> /// Evaluates the binding. /// </summary> public virtual object Evaluate(DotvvmBindableObject control, DotvvmProperty property) { return(ExecDelegate(control, property != DotvvmBindableObject.DataContextProperty)); }
/// <summary> /// Registers the specified DotVVM property. /// </summary> public static DotvvmProperty Register <TPropertyType, TDeclaringType>(string propertyName, TPropertyType defaultValue = default(TPropertyType), bool isValueInherited = false, DotvvmProperty property = null) { var field = typeof(TDeclaringType).GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (field == null) { throw new ArgumentException($"'{typeof(TDeclaringType).Name}' does not contain static field '{propertyName}Property'."); } return(Register(propertyName, typeof(TPropertyType), typeof(TDeclaringType), defaultValue, isValueInherited, property, field)); }
public override Type GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, DotvvmBindableObject control, DotvvmProperty property = null) { var controlType = control.GetType(); var controlPropertyField = controlType.GetField($"{PropertyName}Property", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); var controlProperty = (DotvvmProperty)controlPropertyField?.GetValue(null); if (controlProperty == null) { throw new Exception($"The property '{PropertyName}' was not found on control '{controlType}'!"); } if (control.Properties.ContainsKey(controlProperty) && control.HasValueBinding(controlProperty)) { return(control.GetValueBinding(controlProperty).ResultType); } return(controlProperty.PropertyType); }
public override Delegate GetCommandDelegate(DotvvmBindableObject control, DotvvmProperty property) { throw new NotImplementedException(); }
/// <summary> /// Updates the viewModel with the new value. /// </summary> public virtual void UpdateSource(object value, DotvvmBindableObject control, DotvvmProperty property) { ExecUpdateDelegate(control, value, property != DotvvmBindableObject.DataContextProperty); }
public abstract Type GetChildDataContextType(Type dataContext, DataContextStack controlContextStack, DotvvmBindableObject control, DotvvmProperty property = null);
public override Delegate GetCommandDelegate(DotvvmBindableObject control, DotvvmProperty property) { return((Delegate)ExecDelegate(control, true, true)); }