コード例 #1
0
 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);
 }
コード例 #2
0
 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 );
     }
 }
コード例 #3
0
 /// <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;
 }
コード例 #4
0
 /// <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;
 }
コード例 #5
0
 /// <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;
 }
コード例 #6
0
 /// <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;
 }
コード例 #7
0
ファイル: BindingInfo.cs プロジェクト: jacob-ebey/Bindable
 public BindingInfo(string sourcePropertyName, PropertyInfo sourceProperty, IBindingConverter converter)
 {
     SourcePropertyName = sourcePropertyName;
     SourceProperty = sourceProperty;
     Converter = converter;
 }
コード例 #8
0
        /// <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;
        }
コード例 #9
0
 public ReversedConverter(IBindingConverter converter) {
     this.converter = converter;
 }
コード例 #10
0
 public void AddConverter( IBindingConverter converter) {
     RegisterConverter( converter );
     RegisterConverter( new ReversedConverter( converter ) );
 }
コード例 #11
0
 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);
 }