Example #1
0
        public void Unbind()
        {
            if (!IsBound)
            {
                return;
            }

            if (mode == BindingMode.OneWay || mode == BindingMode.TwoWay)
            {
                var notifyInterface = source as INotifyPropertyChanged;
                if (notifyInterface != null)
                {
                    // remove event handler
                    notifyInterface.PropertyChanged -= OnSourcePropertyChanged;
                }
            }

            if (mode == BindingMode.OneWayToSource || mode == BindingMode.TwoWay)
            {
                var notifyInterface = target as INotifyPropertyChanged;
                if (notifyInterface != null)
                {
                    // remove event handler
                    notifyInterface.PropertyChanged -= OnTargetPropertyChanged;
                }
            }

            ResetValue();

            if ((flags & ControlFlags.AutoMatchConverter) != 0)
            {
                // clear converter
                converter = null;
            }

            source         = null;
            sourceProperty = null;
        }
Example #2
0
        public Binding(string sourcePath, object target, string targetPath, BindingMode mode, ConversionMode conversionMode, IValueConverter converter)
        {
            if (target == null)
            {
                Debug.LogError("target is null");
                return;
            }

            if (string.IsNullOrEmpty(sourcePath))
            {
                Debug.LogError("sourcePath is null", target as UnityEngine.Object);
                return;
            }

            if (string.IsNullOrEmpty(targetPath))
            {
                Debug.LogError("targetPath is null", target as UnityEngine.Object);
                return;
            }

            // handle nested path
            var bindingTarget      = BindingUtility.GetBindingObject(target, targetPath);
            var targetPropertyName = BindingUtility.GetPropertyName(targetPath);
            var targetType         = bindingTarget.GetType();

            // get target property accessor
            var targetProperty = TypeCache.Instance.GetPropertyAccessor(targetType, targetPropertyName);

            if (targetProperty == null)
            {
                Debug.LogError(string.Format("Invalid target path {0}", targetPath), target as UnityEngine.Object);
                return;
            }

            // check conversion mode
            if (conversionMode == ConversionMode.Parameter && converter == null)
            {
                Debug.LogError("Converter is null", target as UnityEngine.Object);
                return;
            }

            // set fields
            this.mode               = mode;
            this.sourcePath         = sourcePath;
            this.sourcePropertyName = BindingUtility.GetPropertyName(sourcePath);
            this.target             = bindingTarget;
            this.targetPath         = targetPropertyName;
            this.targetProperty     = targetProperty;

            // setup converter
            if (conversionMode == ConversionMode.Parameter)
            {
                // use specified converter
                this.converter = converter;
            }
            else if (conversionMode == ConversionMode.Automatic)
            {
                // set flag
                flags = ControlFlags.AutoMatchConverter;
            }
        }
Example #3
0
        public void Bind(object source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source is null");
            }

            if (IsBound)
            {
                Unbind();
            }

            // handle nested property
            var bindingSource = BindingUtility.GetBindingObject(source, sourcePath);

            if (bindingSource == null)
            {
                Debug.LogError("bindingSource is null", target as UnityEngine.Object);
                return;
            }

            // get property info
            var sourceType     = bindingSource.GetType();
            var sourceProperty = TypeCache.Instance.GetPropertyAccessor(sourceType, sourcePropertyName);

            if (sourceProperty == null)
            {
                Debug.LogError(string.Format("Invalid source path {0}", sourcePath), target as UnityEngine.Object);
                return;
            }

            // set source
            this.source         = bindingSource;
            this.sourceProperty = sourceProperty;

            if (mode == BindingMode.OneWay || mode == BindingMode.TwoWay)
            {
                var notifyInterface = this.source as INotifyPropertyChanged;
                if (notifyInterface != null)
                {
                    // add event handler
                    notifyInterface.PropertyChanged += OnSourcePropertyChanged;
                }
            }

            if (mode == BindingMode.OneWayToSource || mode == BindingMode.TwoWay)
            {
                var notifyInterface = this.target as INotifyPropertyChanged;
                if (notifyInterface != null)
                {
                    // add event handler
                    notifyInterface.PropertyChanged += OnTargetPropertyChanged;
                }
            }

            if ((flags & ControlFlags.AutoMatchConverter) != 0)
            {
                // get converter
                converter = ValueConverterProvider.Instance.MatchConverter(sourceProperty.PropertyType, targetProperty.PropertyType);
            }

            InitValue();
        }