Beispiel #1
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;
            }
        }
Beispiel #2
0
        public void Bind(object source)
        {
            if (source == null)
            {
                Debug.LogError("source is null", factory as UnityEngine.Object);
                return;
            }

            if (IsBound)
            {
                Unbind();
            }

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

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

            // get collection object
            var collectionObject = BindingUtility.GetPropertyValue(bindingSource, sourcePropertyName);

            if (collectionObject == null)
            {
                Debug.LogError("Binding failed, collectionObject is null", factory as UnityEngine.Object);
                return;
            }

            // check if collection object is IList
            IList collectionSource = collectionObject as IList;

            if (collectionSource == null)
            {
                var msg = string.Format("Binding failed, collection source is not IList object. source={0}, sourcePath={1}, collectionSource={2}",
                                        source, sourcePath, collectionObject.GetType());

                Debug.LogError(msg, factory as UnityEngine.Object);
                return;
            }

            // save source
            this.source      = bindingSource;
            this.itemsSource = collectionSource;

            // register event
            var notifyCollection = itemsSource as INotifyCollectionChanged;

            if (notifyCollection != null)
            {
                notifyCollection.CollectionChanged += OnCollectionChanged;
            }

            // register property event
            var notifyInterface = this.source as INotifyPropertyChanged;

            if (notifyInterface != null)
            {
                notifyInterface.PropertyChanged += OnSourcePropertyChanged;
            }

            ResetItems();

            UpdateView();
        }
Beispiel #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();
        }