internal static BindableProperty Create <TDeclarer, TPropertyType>(Expression <Func <TDeclarer, TPropertyType> > getter, TPropertyType defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate <TPropertyType> validateValue, BindingPropertyChangedDelegate <TPropertyType> propertyChanged, BindingPropertyChangingDelegate <TPropertyType> propertyChanging, CoerceValueDelegate <TPropertyType> coerceValue, BindablePropertyBindingChanging bindingChanging, bool isReadOnly = false, CreateDefaultValueDelegate <TDeclarer, TPropertyType> defaultValueCreator = null) where TDeclarer : BindableObject { if (getter == null) { throw new ArgumentNullException(nameof(getter)); } Expression expr = getter.Body; var unary = expr as UnaryExpression; if (unary != null) { expr = unary.Operand; } var member = expr as MemberExpression; if (member == null) { throw new ArgumentException("getter must be a MemberExpression", nameof(getter)); } var property = (PropertyInfo)member.Member; ValidateValueDelegate untypedValidateValue = null; BindingPropertyChangedDelegate untypedBindingPropertyChanged = null; BindingPropertyChangingDelegate untypedBindingPropertyChanging = null; CoerceValueDelegate untypedCoerceValue = null; CreateDefaultValueDelegate untypedDefaultValueCreator = null; if (validateValue != null) { untypedValidateValue = (bindable, value) => validateValue(bindable, (TPropertyType)value); } if (propertyChanged != null) { untypedBindingPropertyChanged = (bindable, oldValue, newValue) => propertyChanged(bindable, (TPropertyType)oldValue, (TPropertyType)newValue); } if (propertyChanging != null) { untypedBindingPropertyChanging = (bindable, oldValue, newValue) => propertyChanging(bindable, (TPropertyType)oldValue, (TPropertyType)newValue); } if (coerceValue != null) { untypedCoerceValue = (bindable, value) => coerceValue(bindable, (TPropertyType)value); } if (defaultValueCreator != null) { untypedDefaultValueCreator = o => defaultValueCreator((TDeclarer)o); } return(new BindableProperty(property.Name, property.PropertyType, typeof(TDeclarer), defaultValue, defaultBindingMode, untypedValidateValue, untypedBindingPropertyChanged, untypedBindingPropertyChanging, untypedCoerceValue, bindingChanging, isReadOnly, untypedDefaultValueCreator)); }
/// <summary> /// Initializes a new instance of the <see cref="DependencyProperty"/> class. /// </summary> /// <param name="declaringType">The type which declares the property.</param> /// <param name="ownerType">The type which can own this property.</param> /// <param name="propertyType">The type of the property.</param> /// <param name="name">The name of the property.</param> /// <param name="defaultBindingMode">The default <see cref="BindingMode"/> for this property.</param> /// <param name="isAttachement">Specifies if the property is a attachement.</param> /// <param name="createValueCallback">The default value of the property.</param> /// <param name="coerceValueCallback">The callback for coercing the value.</param> /// <param name="validationCallback">The validation callback of the value.</param> /// <param name="changingCallback">The changing callback of the property.</param> /// <param name="changedCallback">The changed callback of the property.</param> internal DependencyProperty(TypeInfo declaringType, String name, BindingMode defaultBindingMode, Boolean isAttachement, CreateValueDelegate <TOwner, TProperty> createValueCallback, CoerceValueDelegate <TOwner, TProperty> coerceValueCallback, PropertyValidationDelegate <TOwner, TProperty> validationCallback, PropertyChangingDelegate <TOwner, TProperty> changingCallback, PropertyChangedDelegate <TOwner, TProperty> changedCallback) : base(declaringType, Reflection.TypeOf <TOwner> .TypeInfo, name, defaultBindingMode, isAttachement, false) { _CreateValueCallback = createValueCallback; _CoerceValueCallback = coerceValueCallback; _ValidationCallback = validationCallback; _ChangingCallback = changingCallback; _ChangedCallback = changedCallback; }
/// <summary> /// Initializes a new instance of the <see cref="DependencyProperty"/> class. /// </summary> /// <param name="declaringType">The type which declares the property.</param> /// <param name="ownerType">The type which can own this property.</param> /// <param name="propertyType">The type of the property.</param> /// <param name="name">The name of the property.</param> /// <param name="defaultBindingMode">The default <see cref="BindingMode"/> for this property.</param> /// <param name="isAttachement">Specifies if the property is a attachement.</param> /// <param name="defaultValue">The default value of the property.</param> /// <param name="coerceValueCallback">The callback for coercing the value.</param> /// <param name="validationCallback">The validation callback of the value.</param> /// <param name="changingCallback">The changing callback of the property.</param> /// <param name="changedCallback">The changed callback of the property.</param> internal DependencyProperty(TypeInfo declaringType, String name, BindingMode defaultBindingMode, Boolean isAttachement, TProperty defaultValue, CoerceValueDelegate <TOwner, TProperty> coerceValueCallback, PropertyValidationDelegate <TOwner, TProperty> validationCallback, PropertyChangingDelegate <TOwner, TProperty> changingCallback, PropertyChangedDelegate <TOwner, TProperty> changedCallback, out DependencyPropertyKey <TProperty> key) : base(declaringType, Reflection.TypeOf <TOwner> .TypeInfo, name, defaultBindingMode, isAttachement, false) { _DefaultValue = defaultValue; _CoerceValueCallback = coerceValueCallback; _ValidationCallback = validationCallback; _ChangingCallback = changingCallback; _ChangedCallback = changedCallback; Key = key = new DependencyPropertyKey <TProperty>(this); }
BindableProperty(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay, ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null, CoerceValueDelegate coerceValue = null, BindablePropertyBindingChanging bindingChanging = null, bool isReadOnly = false, CreateDefaultValueDelegate defaultValueCreator = null) { if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } if (ReferenceEquals(returnType, null)) { throw new ArgumentNullException(nameof(returnType)); } if (ReferenceEquals(declaringType, null)) { throw new ArgumentNullException(nameof(declaringType)); } // don't use Enum.IsDefined as its redonkulously expensive for what it does if (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime) { throw new ArgumentException($"Not a valid type of BindingMode. Property: {returnType} {declaringType.Name}.{propertyName}. Default binding mode: {defaultBindingMode}", nameof(defaultBindingMode)); } if (defaultValue == null && Nullable.GetUnderlyingType(returnType) == null && returnType.GetTypeInfo().IsValueType) { defaultValue = Activator.CreateInstance(returnType); } if (defaultValue != null && !returnType.IsInstanceOfType(defaultValue)) { throw new ArgumentException($"Default value did not match return type. Property: {returnType} {declaringType.Name}.{propertyName} Default value type: {defaultValue.GetType().Name}, ", nameof(defaultValue)); } if (defaultBindingMode == BindingMode.Default) { defaultBindingMode = BindingMode.OneWay; } PropertyName = propertyName; ReturnType = returnType; ReturnTypeInfo = returnType.GetTypeInfo(); DeclaringType = declaringType; DefaultValue = defaultValue; DefaultBindingMode = defaultBindingMode; PropertyChanged = propertyChanged; PropertyChanging = propertyChanging; ValidateValue = validateValue; CoerceValue = coerceValue; BindingChanging = bindingChanging; IsReadOnly = isReadOnly; DefaultValueCreator = defaultValueCreator; }
/// <summary> /// Create bindinble property simpler. /// </summary> /// <typeparam name="TDeclarer">Type of the declarer.</typeparam> /// <typeparam name="TPropertyType">Type of the property.</typeparam> /// <param name="propertyName">Property name.</param> /// <param name="defaultValue">Default value.</param> /// <param name="defaultBindingMode">Default binding mode.</param> /// <param name="validateValue">Validate value.</param> /// <param name="propertyChanged">Property changed delegate.</param> /// <param name="propertyChanging">Property changing delegate.</param> /// <param name="coerceValue">Coerce value.</param> /// <param name="defaultValueCreator">Default value creator.</param> /// <returns>Bindable property.</returns> public static BindableProperty Create <TDeclarer, TPropertyType>( string propertyName, TPropertyType defaultValue = default(TPropertyType), BindingMode defaultBindingMode = BindingMode.OneWay, ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null, CoerceValueDelegate coerceValue = null, CreateDefaultValueDelegate defaultValueCreator = null) { return(BindableProperty.Create( propertyName, typeof(TPropertyType), typeof(TDeclarer), defaultValue: defaultValue, defaultBindingMode: defaultBindingMode, validateValue: validateValue, propertyChanged: propertyChanged, propertyChanging: propertyChanging, coerceValue: coerceValue, defaultValueCreator: defaultValueCreator)); }
internal static BindableProperty CreateAttached(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue, BindingPropertyChangedDelegate propertyChanged, BindingPropertyChangingDelegate propertyChanging, CoerceValueDelegate coerceValue, BindablePropertyBindingChanging bindingChanging, bool isReadOnly, CreateDefaultValueDelegate defaultValueCreator = null) { return(new BindableProperty(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, bindingChanging, isReadOnly, defaultValueCreator)); }
public static BindableProperty CreateAttached(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay, ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null, CoerceValueDelegate coerceValue = null, CreateDefaultValueDelegate defaultValueCreator = null) { return(CreateAttached(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, null, false, defaultValueCreator)); }
internal static BindableProperty CreateAttached <TDeclarer, TPropertyType>(Expression <Func <BindableObject, TPropertyType> > staticgetter, TPropertyType defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate <TPropertyType> validateValue, BindingPropertyChangedDelegate <TPropertyType> propertyChanged, BindingPropertyChangingDelegate <TPropertyType> propertyChanging, CoerceValueDelegate <TPropertyType> coerceValue, BindablePropertyBindingChanging bindingChanging, bool isReadOnly = false, CreateDefaultValueDelegate <BindableObject, TPropertyType> defaultValueCreator = null) { if (staticgetter == null) { throw new ArgumentNullException("staticgetter"); } Expression expr = staticgetter.Body; var unary = expr as UnaryExpression; if (unary != null) { expr = unary.Operand; } var methodcall = expr as MethodCallExpression; if (methodcall == null) { throw new ArgumentException("staticgetter must be a MethodCallExpression", "staticgetter"); } MethodInfo method = methodcall.Method; if (!method.Name.StartsWith("Get", StringComparison.Ordinal)) { throw new ArgumentException("staticgetter name must start with Get", "staticgetter"); } string propertyname = method.Name.Substring(3); ValidateValueDelegate untypedValidateValue = null; BindingPropertyChangedDelegate untypedBindingPropertyChanged = null; BindingPropertyChangingDelegate untypedBindingPropertyChanging = null; CoerceValueDelegate untypedCoerceValue = null; CreateDefaultValueDelegate untypedDefaultValueCreator = null; if (validateValue != null) { untypedValidateValue = (bindable, value) => validateValue(bindable, (TPropertyType)value); } if (propertyChanged != null) { untypedBindingPropertyChanged = (bindable, oldValue, newValue) => propertyChanged(bindable, (TPropertyType)oldValue, (TPropertyType)newValue); } if (propertyChanging != null) { untypedBindingPropertyChanging = (bindable, oldValue, newValue) => propertyChanging(bindable, (TPropertyType)oldValue, (TPropertyType)newValue); } if (coerceValue != null) { untypedCoerceValue = (bindable, value) => coerceValue(bindable, (TPropertyType)value); } if (defaultValueCreator != null) { untypedDefaultValueCreator = o => defaultValueCreator(o); } return(new BindableProperty(propertyname, method.ReturnType, typeof(TDeclarer), defaultValue, defaultBindingMode, untypedValidateValue, untypedBindingPropertyChanged, untypedBindingPropertyChanging, untypedCoerceValue, bindingChanging, isReadOnly, untypedDefaultValueCreator)); }
public static BindablePropertyKey CreateAttachedReadOnly <TDeclarer, TPropertyType>(Expression <Func <BindableObject, TPropertyType> > staticgetter, TPropertyType defaultValue, BindingMode defaultBindingMode = BindingMode.OneWayToSource, ValidateValueDelegate <TPropertyType> validateValue = null, BindingPropertyChangedDelegate <TPropertyType> propertyChanged = null, BindingPropertyChangingDelegate <TPropertyType> propertyChanging = null, CoerceValueDelegate <TPropertyType> coerceValue = null, CreateDefaultValueDelegate <BindableObject, TPropertyType> defaultValueCreator = null) { return (new BindablePropertyKey(CreateAttached <TDeclarer, TPropertyType>(staticgetter, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, null, true, defaultValueCreator))); }
public static BindableProperty Create <TDeclarer, TPropertyType>(Expression <Func <TDeclarer, TPropertyType> > getter, TPropertyType defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay, ValidateValueDelegate <TPropertyType> validateValue = null, BindingPropertyChangedDelegate <TPropertyType> propertyChanged = null, BindingPropertyChangingDelegate <TPropertyType> propertyChanging = null, CoerceValueDelegate <TPropertyType> coerceValue = null, CreateDefaultValueDelegate <TDeclarer, TPropertyType> defaultValueCreator = null) where TDeclarer : BindableObject { return(Create(getter, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, null, defaultValueCreator: defaultValueCreator)); }
public static DependencyPropertyKey <TProperty> CreateReadonlyAttachment <TDeclarer, TOwner, TProperty>(String name, CreateValueDelegate <TOwner, TProperty> defaultValueFactory, BindingMode bindingMode = BindingMode.Default, PropertyValidationDelegate <TOwner, TProperty> validation = null, PropertyChangingDelegate <TOwner, TProperty> propertyChanging = null, PropertyChangedDelegate <TOwner, TProperty> propertyChanged = null, CoerceValueDelegate <TOwner, TProperty> coerceValue = null) where TOwner : class, IDependencyObject { ValidatePropertyName(name); new DependencyProperty <TProperty, TOwner>(TypeOf <TDeclarer> .TypeInfo, name, bindingMode, true, defaultValueFactory, coerceValue, validation, propertyChanging, propertyChanged, out DependencyPropertyKey <TProperty> key); return(key); }
public static DependencyPropertyKey <TProperty> CreateReadonly <TDeclarer, TProperty>(Expression <Func <TDeclarer, TProperty> > getter, CreateValueDelegate <TDeclarer, TProperty> defaultValueFactory, BindingMode bindingMode = BindingMode.Default, PropertyValidationDelegate <TDeclarer, TProperty> validation = null, PropertyChangingDelegate <TDeclarer, TProperty> propertyChanging = null, PropertyChangedDelegate <TDeclarer, TProperty> propertyChanged = null, CoerceValueDelegate <TDeclarer, TProperty> coerceValue = null) where TDeclarer : class, IDependencyObject { new DependencyProperty <TProperty, TDeclarer>(TypeOf <TDeclarer> .TypeInfo, GetPropertyName <TDeclarer, TProperty>(getter), bindingMode, true, defaultValueFactory, coerceValue, validation, propertyChanging, propertyChanged, out DependencyPropertyKey <TProperty> key); return(key); }
public static DependencyProperty <TProperty> Create <TDeclarer, TProperty>(Expression <Func <TDeclarer, TProperty> > getter, TProperty defaultValue = default(TProperty), BindingMode bindingMode = BindingMode.Default, PropertyValidationDelegate <TDeclarer, TProperty> validation = null, PropertyChangingDelegate <TDeclarer, TProperty> propertyChanging = null, PropertyChangedDelegate <TDeclarer, TProperty> propertyChanged = null, CoerceValueDelegate <TDeclarer, TProperty> coerceValue = null) where TDeclarer : class, IDependencyObject { return(new DependencyProperty <TProperty, TDeclarer>(TypeOf <TDeclarer> .TypeInfo, GetPropertyName <TDeclarer, TProperty>(getter), bindingMode, false, defaultValue, coerceValue, validation, propertyChanging, propertyChanged)); }
BindableProperty(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay, ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null, CoerceValueDelegate coerceValue = null, BindablePropertyBindingChanging bindingChanging = null, bool isReadOnly = false, CreateDefaultValueDelegate defaultValueCreator = null) { if (propertyName == null) { throw new ArgumentNullException("propertyName"); } if (ReferenceEquals(returnType, null)) { throw new ArgumentNullException("returnType"); } if (ReferenceEquals(declaringType, null)) { throw new ArgumentNullException("declaringType"); } // don't use Enum.IsDefined as its redonkulously expensive for what it does if (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime) { throw new ArgumentException("Not a valid type of BindingMode", "defaultBindingMode"); } if (defaultValue == null && Nullable.GetUnderlyingType(returnType) == null && returnType.GetTypeInfo().IsValueType) { throw new ArgumentException("Not a valid default value", "defaultValue"); } if (defaultValue != null && !returnType.IsInstanceOfType(defaultValue)) { throw new ArgumentException("Default value did not match return type", "defaultValue"); } if (defaultBindingMode == BindingMode.Default) { defaultBindingMode = BindingMode.OneWay; } PropertyName = propertyName; ReturnType = returnType; ReturnTypeInfo = returnType.GetTypeInfo(); DeclaringType = declaringType; DefaultValue = defaultValue; DefaultBindingMode = defaultBindingMode; PropertyChanged = propertyChanged; PropertyChanging = propertyChanging; ValidateValue = validateValue; CoerceValue = coerceValue; BindingChanging = bindingChanging; IsReadOnly = isReadOnly; DefaultValueCreator = defaultValueCreator; Dictionary <string, BindableProperty> nameToBindableProperty; bindablePropertyOfType.TryGetValue(declaringType, out nameToBindableProperty); if (null == nameToBindableProperty) { nameToBindableProperty = new Dictionary <string, BindableProperty>(); bindablePropertyOfType.Add(declaringType, nameToBindableProperty); } if (!nameToBindableProperty.ContainsKey(propertyName)) { nameToBindableProperty.Add(propertyName, this); } else { nameToBindableProperty[propertyName] = this; } }
internal static BindableProperty Create(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue, BindingPropertyChangedDelegate propertyChanged, BindingPropertyChangingDelegate propertyChanging, CoerceValueDelegate coerceValue, BindablePropertyBindingChanging bindingChanging, CreateDefaultValueDelegate defaultValueCreator = null) { return(new BindableProperty(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, bindingChanging, defaultValueCreator: defaultValueCreator)); }