public ViewVisibilityBinding(View target, INotifyPropertyChanged sourceObject, string sourceProperty, IBindingConverter<bool?, ViewStates> converter) : base(target) { _target = target; var valueConverter = converter != null ? converter : new VisibleIfTrueConverter(); _propertyBinding = new PropertyBinding<ViewStates, bool?>(this, "Visibility", sourceObject, sourceProperty, valueConverter); }
private void RegisterConverter(IBindingConverter converter) { Type first = converter.FirstType; Type second = converter.SecondType; if (converters.ContainsKey( first )) { Dictionary< Type, IBindingConverter > firstClassConverters = converters[ first ]; if (firstClassConverters.ContainsKey( second )) { throw new ApplicationException( String.Format( "Converter for {0} -> {1} classes is already registered.", first.Name, second.Name ) ); } firstClassConverters.Add( second, converter ); } else { Dictionary<Type, IBindingConverter> firstClassConverters = new Dictionary<Type, IBindingConverter>(); firstClassConverters.Add( second, converter ); converters.Add( first, firstClassConverters ); } }
public BindingConverterCollection Add(Type fieldType, IBindingConverter converter) { Ensure.NotNull(fieldType, "fieldType"); Ensure.NotNull(converter, "converter"); string key = GetKey(fieldType); List <IBindingConverter> list; if (!Storage.TryGetValue(key, out list)) { list = new List <IBindingConverter>(); Storage.Add(key, list); } list.Add(converter); return(this); }
private void RegisterConverter(IBindingConverter converter) { Type first = converter.FirstType; Type second = converter.SecondType; if (converters.ContainsKey(first)) { Dictionary <Type, IBindingConverter> firstClassConverters = converters[first]; if (firstClassConverters.ContainsKey(second)) { throw new ApplicationException(String.Format("Converter for {0} -> {1} classes is already registered.", first.Name, second.Name)); } firstClassConverters.Add(second, converter); } else { Dictionary <Type, IBindingConverter> firstClassConverters = new Dictionary <Type, IBindingConverter>(); firstClassConverters.Add(second, converter); converters.Add(first, firstClassConverters); } }
/// <summary> /// Bind properties of two objects bidirectionally, converting the values using a converter /// </summary> /// <param name="object1">Object containing 1st property to bind</param> /// <param name="property1">Property of 1st object to bind</param> /// <param name="object2">Object containing 2nd property to bind</param> /// <param name="property2">Property of 2nd object to bind</param> /// <param name="converter">taking care of converting the synchronzied value to the correct target format and back</param> public BidirectionalBinding(INotifyPropertyChanged object1, string property1, INotifyPropertyChanged object2, string property2, IBindingConverter converter) : this(object1, property1, object2, property2) { this.converter = converter; }
/// <summary> /// Sets a binding. Currently only one way bindings are supported. /// </summary> /// <param name="bindableProperty">The property to bind to.</param> /// <param name="sourcePropertyName">the name of the data context property to get the value from.</param> /// <param name="converter">Converter to convert between object and the type the BindableProperty expects.</param> public void SetBinding(BindableProperty <TOwner> bindableProperty, string sourcePropertyName, IBindingConverter converter = null) { lock (syncRoot) { if (bindingInfoCache.ContainsKey(bindableProperty)) { throw new Exception("Can not bind to the same property more than once."); } if (bindingPropertyCache.ContainsKey(sourcePropertyName)) { bindingPropertyCache[sourcePropertyName].Add(bindableProperty); } else { bindingPropertyCache.Add(sourcePropertyName, new List <BindableProperty <TOwner> >(new[] { bindableProperty })); } UpdateBinding(bindableProperty, sourcePropertyName, converter); } }
public BindingInfo(string sourcePropertyName, PropertyInfo sourceProperty, IBindingConverter converter) { SourcePropertyName = sourcePropertyName; SourceProperty = sourceProperty; Converter = converter; }
/// <summary> /// Bind properties of two objects bidirectionally, converting the values using a converter. /// Synchronization can be intercepted by adding a validator. /// </summary> /// <param name="object1">Object containing 1st property to bind</param> /// <param name="property1">Property of 1st object to bind</param> /// <param name="object2">Object containing 2nd property to bind</param> /// <param name="property2">Property of 2nd object to bind</param> /// <param name="converter">taking care of converting the synchronzied value to the correct target format and back</param> /// <param name="validator">validator to intercept synchronisation if the value does not match certain criteria</param> public BidirectionalBinding(INotifyPropertyChanged object1, string property1, INotifyPropertyChanged object2, string property2, IBindingConverter converter, IBindingValidator validator) : this(object1, property1, object2, property2, converter) { this.validator = validator; }
public ViewClickableBinding(View target, INotifyPropertyChanged sourceObject, string sourceProperty, IBindingConverter<bool, bool> converter) : base(target) { _target = target; _propertyBinding = new PropertyBinding<bool, bool>(this, "Clickable", sourceObject, sourceProperty, converter); }
/// <summary> /// Set a binding on the object. /// </summary> /// <param name="property">The property to bind to.</param> /// <param name="sourcePropertyName">The name of the data context property to bind to.</param> /// <param name="converter">The converter.</param> public void SetBinding(BindableProperty <TestBindableObject> property, string sourcePropertyName, IBindingConverter converter = null) { manager.SetBinding(property, sourcePropertyName, converter); }
/// <summary> /// Set a binding on the object. /// </summary> /// <param name="property">The property to bind to.</param> /// <param name="sourcePropertyName">The name of the data context property to bind to.</param> /// <param name="converter">The converter.</param> public void SetBinding(BindableProperty <BindableListView> property, string sourcePropertyName, IBindingConverter converter = null) { manager.SetBinding(property, sourcePropertyName, converter); }
/// <summary> /// Bind properties of two objects bidirectionally, converting the values using a converter. /// Synchronization can be intercepted by adding a validator. /// </summary> /// <param name="controlObject">Object containing 1st property to bind</param> /// <param name="controlPropertyName">Property of 1st object to bind</param> /// <param name="fieldObject">Object containing 2nd property to bind</param> /// <param name="fieldPropertyName">Property of 2nd object to bind</param> /// <param name="converter">taking care of converting the synchronzied value to the correct target format and back</param> /// <param name="validator">validator to intercept synchronisation if the value does not match certain criteria</param> public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingConverter converter, IBindingValidator validator) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName, converter) { this.validator = validator; }
public ReversedConverter(IBindingConverter converter) { this.converter = converter; }
public void AddConverter(IBindingConverter converter) { RegisterConverter(converter); RegisterConverter(new ReversedConverter(converter)); }
private void UpdateBinding(BindableProperty <TOwner> bindableProperty, string sourcePropertyName, IBindingConverter converter) { PropertyInfo propertyInfo = null; if (bindableObject.DataContext != null) { propertyInfo = bindableObject.DataContext.GetType().GetRuntimeProperty(sourcePropertyName); } BindingInfo info = new BindingInfo(sourcePropertyName, propertyInfo, converter); lock (syncRoot) { if (bindingInfoCache.ContainsKey(bindableProperty)) { bindingInfoCache[bindableProperty] = info; } else { bindingInfoCache.Add(bindableProperty, info); } } }
/// <summary> /// Bind properties of two objects bidirectionally, converting the values using a converter /// </summary> /// <param name="controlObject">Object containing 1st property to bind</param> /// <param name="controlPropertyName">Property of 1st object to bind</param> /// <param name="fieldObject">Object containing 2nd property to bind</param> /// <param name="fieldPropertyName">Property of 2nd object to bind</param> /// <param name="converter">taking care of converting the synchronzied value to the correct target format and back</param> public BidirectionalBinding(INotifyPropertyChanged controlObject, string controlPropertyName, INotifyPropertyChanged fieldObject, string fieldPropertyName, IBindingConverter converter) : this(controlObject, controlPropertyName, fieldObject, fieldPropertyName) { this.converter = converter; }
internal TypedBinding(int id, TSource source, Expression <Func <TSource, TSourceProp> > sourceProperty, TTarget target, Expression <Func <TTarget, TTargetProp> > targetProperty, TSourceProp fallback, BindingMode mode, IBindingConverter converter) : base(id, typeof(TSource), typeof(TTarget)) { if (converter == null && typeof(TSourceProp) != typeof(TTargetProp)) { throw new ArgumentException("Unable to create binding because " + $"{typeof(TTargetProp)} and {typeof(TSourceProp)} " + "Are not the same type and no converter was specified"); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } if (sourceProperty == null) { throw new ArgumentNullException(nameof(sourceProperty)); } if (targetProperty == null) { throw new ArgumentNullException(nameof(targetProperty)); } _fallback = fallback == null ? default(TSourceProp) : fallback; //Retrieving the MemberExpression objects indicated by the expressions provided to the constructor is vital to //Constructing the delegates to getter/setter methods for the bound properties MemberExpression sourceMember = GetMemberExpression(sourceProperty.Body); MemberExpression targetMember = GetMemberExpression(targetProperty.Body); _sourcePropName = sourceMember.Member.Name; _targetPropName = targetMember.Member.Name; _sourceObject = new WeakReference(source); _targetObject = new WeakReference(target); _converter = converter; _mode = mode; //Which delegates need to be created and which events need subscription depends on the nature of the binding if (BindingMode == BindingMode.OneWay || BindingMode == BindingMode.TwoWay) { _sourcePropertyGet = (Func <TSource, TSourceProp>)CreateDelegate <TSource, TSourceProp>(sourceMember, DelegateType.Get); _targetPropertySet = (Action <TTarget, TTargetProp>)CreateDelegate <TTarget, TTargetProp>(targetMember, DelegateType.Set); PropertyChangedEventManager.AddHandler(source, this.OnSourceChanged, sourceMember.Member.Name); //This step simply ensures that the properties are put in sync once the binding is made OnSourceChanged(source, new PropertyChangedEventArgs(_sourcePropName)); } if (BindingMode == BindingMode.OneWayToSource || BindingMode == BindingMode.TwoWay) { _sourcePropertySet = (Action <TSource, TSourceProp>)CreateDelegate <TSource, TSourceProp>(sourceMember, DelegateType.Set); _targetPropertyGet = (Func <TTarget, TTargetProp>)CreateDelegate <TTarget, TTargetProp>(targetMember, DelegateType.Get); PropertyChangedEventManager.AddHandler(target, this.OnTargetChanged, targetMember.Member.Name); //Make sure the properties are put in sync once the binding is made if (BindingMode == BindingMode.OneWayToSource) { OnTargetChanged(target, new PropertyChangedEventArgs(_targetPropName)); } } }
public void WithConverterFor <T>(IBindingConverter conv) { Type typeToBind = typeof(T); ConverterList.Add(typeToBind.Name, conv); }
public void AddConverter( IBindingConverter converter) { RegisterConverter( converter ); RegisterConverter( new ReversedConverter( converter ) ); }
/// <summary> /// Connects Source and Target objects. /// </summary> public void Bind() { // Resolve binding mode and search converter if need if (needAdapterAnyway) { if (adapter == null) adapter = settings.GetAdapterFor(target.GetType()); realMode = mode == BindingMode.Default ? adapter.DefaultMode : mode; } else { realMode = mode == BindingMode.Default ? BindingMode.TwoWay : mode; if (realMode == BindingMode.TwoWay || realMode == BindingMode.OneWayToSource) { if (! (target is INotifyPropertyChanged)) if (adapter == null) adapter = settings.GetAdapterFor( target.GetType() ); } } // Get properties info and check if they are collections sourcePropertyInfo = source.GetType( ).GetProperty( sourceProperty ); if ( null == adapter ) targetPropertyInfo = target.GetType( ).GetProperty( targetProperty ); Type targetPropertyClass = (null == adapter) ? targetPropertyInfo.PropertyType : adapter.GetTargetPropertyClazz(targetProperty); sourceIsObservable = typeof(IObservableList).IsAssignableFrom( sourcePropertyInfo.PropertyType ); targetIsObservable = typeof(IObservableList).IsAssignableFrom(targetPropertyClass); // We need converter if data will flow from non-observable property to property of another class if (targetPropertyClass != sourcePropertyInfo.PropertyType) { bool needConverter = false; if (realMode == BindingMode.OneTime || realMode == BindingMode.OneWay || realMode == BindingMode.TwoWay) needConverter |= !sourceIsObservable; if (realMode == BindingMode.OneWayToSource || realMode == BindingMode.TwoWay) needConverter |= !targetIsObservable; // if (needConverter) { if ( converter == null ) converter = settings.GetConverterFor( targetPropertyClass, sourcePropertyInfo.PropertyType ); else { // Check if converter must be reversed if ( converter.FirstType.IsAssignableFrom( targetPropertyClass ) && converter.SecondType.IsAssignableFrom( sourcePropertyInfo.PropertyType ) ) { // Nothing to do, it's ok } else if ( converter.SecondType.IsAssignableFrom( targetPropertyClass ) && converter.FirstType.IsAssignableFrom( sourcePropertyInfo.PropertyType ) ) { // Should be reversed converter = new ReversedConverter( converter ); } else { throw new Exception("Provided converter doesn't support conversion between " + "specified properties."); } } if (converter == null ) throw new Exception( String.Format("Converter for {0} -> {1} classes not found.", targetPropertyClass.Name, sourcePropertyInfo.PropertyType.Name) ); } } // Verify properties getters and setters for specified binding mode if (realMode == BindingMode.OneTime || realMode == BindingMode.OneWay || realMode == BindingMode.TwoWay) { if (sourcePropertyInfo.GetGetMethod() == null) throw new Exception( "Source property getter not found" ); if (sourceIsObservable) { if (null == adapter && targetPropertyInfo.GetGetMethod() == null) throw new Exception( "Target property getter not found" ); if (!typeof(IList).IsAssignableFrom( targetPropertyClass )) throw new Exception( "Target property class have to implement IList" ); } else { if (null == adapter && targetPropertyInfo.GetSetMethod() == null) throw new Exception( "Target property setter not found" ); } } if (realMode == BindingMode.OneWayToSource || realMode == BindingMode.TwoWay) { if ( null == adapter && targetPropertyInfo.GetGetMethod() == null) throw new Exception( "Target property getter not found" ); if ( targetIsObservable) { if (sourcePropertyInfo.GetGetMethod() == null) throw new Exception( "Source property getter not found" ); if (!typeof(IList).IsAssignableFrom( sourcePropertyInfo.PropertyType )) throw new Exception( "Source property class have to implement IList" ); } else { if (sourcePropertyInfo.GetSetMethod() == null ) throw new Exception( "Source property setter not found" ); } } // Subscribe to listeners ConnectSourceAndTarget(); // Initial flush values if ( realMode == BindingMode.OneTime || realMode == BindingMode.OneWay || realMode == BindingMode.TwoWay) UpdateTarget(); if (realMode == BindingMode.OneWayToSource || realMode == BindingMode.TwoWay) UpdateSource(); this.bound = true; }
/// <summary> /// Connects Source and Target objects. /// </summary> public void Bind() { // Resolve binding mode and search converter if need if (needAdapterAnyway) { if (adapter == null) { adapter = settings.GetAdapterFor(target.GetType()); } realMode = mode == BindingMode.Default ? adapter.DefaultMode : mode; } else { realMode = mode == BindingMode.Default ? BindingMode.TwoWay : mode; if (realMode == BindingMode.TwoWay || realMode == BindingMode.OneWayToSource) { if (!(target is INotifyPropertyChanged)) { if (adapter == null) { adapter = settings.GetAdapterFor(target.GetType()); } } } } // Get properties info and check if they are collections sourcePropertyInfo = source.GetType( ).GetProperty(sourceProperty); if (null == adapter) { targetPropertyInfo = target.GetType( ).GetProperty(targetProperty); } Type targetPropertyClass = (null == adapter) ? targetPropertyInfo.PropertyType : adapter.GetTargetPropertyClazz(targetProperty); sourceIsObservable = typeof(IObservableList).IsAssignableFrom(sourcePropertyInfo.PropertyType); targetIsObservable = typeof(IObservableList).IsAssignableFrom(targetPropertyClass); // We need converter if data will flow from non-observable property to property of another class if (targetPropertyClass != sourcePropertyInfo.PropertyType) { bool needConverter = false; if (realMode == BindingMode.OneTime || realMode == BindingMode.OneWay || realMode == BindingMode.TwoWay) { needConverter |= !sourceIsObservable; } if (realMode == BindingMode.OneWayToSource || realMode == BindingMode.TwoWay) { needConverter |= !targetIsObservable; } // if (needConverter) { if (converter == null) { converter = settings.GetConverterFor(targetPropertyClass, sourcePropertyInfo.PropertyType); } else { // Check if converter must be reversed if (converter.FirstType.IsAssignableFrom(targetPropertyClass) && converter.SecondType.IsAssignableFrom(sourcePropertyInfo.PropertyType)) { // Nothing to do, it's ok } else if (converter.SecondType.IsAssignableFrom(targetPropertyClass) && converter.FirstType.IsAssignableFrom(sourcePropertyInfo.PropertyType)) { // Should be reversed converter = new ReversedConverter(converter); } else { throw new Exception("Provided converter doesn't support conversion between " + "specified properties."); } } if (converter == null) { throw new Exception(String.Format("Converter for {0} -> {1} classes not found.", targetPropertyClass.Name, sourcePropertyInfo.PropertyType.Name)); } } } // Verify properties getters and setters for specified binding mode if (realMode == BindingMode.OneTime || realMode == BindingMode.OneWay || realMode == BindingMode.TwoWay) { if (sourcePropertyInfo.GetGetMethod() == null) { throw new Exception("Source property getter not found"); } if (sourceIsObservable) { if (null == adapter && targetPropertyInfo.GetGetMethod() == null) { throw new Exception("Target property getter not found"); } if (!typeof(IList).IsAssignableFrom(targetPropertyClass)) { throw new Exception("Target property class have to implement IList"); } } else { if (null == adapter && targetPropertyInfo.GetSetMethod() == null) { throw new Exception("Target property setter not found"); } } } if (realMode == BindingMode.OneWayToSource || realMode == BindingMode.TwoWay) { if (null == adapter && targetPropertyInfo.GetGetMethod() == null) { throw new Exception("Target property getter not found"); } if (targetIsObservable) { if (sourcePropertyInfo.GetGetMethod() == null) { throw new Exception("Source property getter not found"); } if (!typeof(IList).IsAssignableFrom(sourcePropertyInfo.PropertyType)) { throw new Exception("Source property class have to implement IList"); } } else { if (sourcePropertyInfo.GetSetMethod() == null) { throw new Exception("Source property setter not found"); } } } // Subscribe to listeners ConnectSourceAndTarget(); // Initial flush values if (realMode == BindingMode.OneTime || realMode == BindingMode.OneWay || realMode == BindingMode.TwoWay) { UpdateTarget(); } if (realMode == BindingMode.OneWayToSource || realMode == BindingMode.TwoWay) { UpdateSource(); } this.bound = true; }