Example #1
0
        public void Bind(BindingType bindingType, INotifyingObject <T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (Bound)
            {
                throw new InvalidOperationException(AlreadyBoundMessage);
            }

            _binding         = _bindingFactory.CreatePropertyBinding(bindingType, this, source, UnityValueConverter <T> .Instance);
            _binding.Culture = _culture;
        }
Example #2
0
        public PropertyBinding(
            BindingType bindingType,
            [NotNull] IDependencyProperty <TTarget> target,
            [NotNull] INotifyingObject <TSource> source,
            [NotNull] ValueConverter <TSource, TTarget> converter)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            _target    = target;
            _source    = source;
            _converter = converter;

            switch (bindingType)
            {
            case BindingType.OneWay:
                _source.PropertyChanged += SourceOnPropertyChanged;
                SetTarget(_source.GetValue());
                break;

            case BindingType.TwoWay:
                _source.PropertyChanged += SourceOnPropertyChanged;
                _target.PropertyChanged += TargetOnPropertyChanged;
                SetTarget(_source.GetValue());
                SetSource(_target.GetValue());
                break;

            case BindingType.OneWayToSource:
                _target.PropertyChanged += TargetOnPropertyChanged;
                SetSource(_target.GetValue());
                break;

            default:
                throw new NotSupportedException(string.Format(
                                                    "The binding type '{0}' is not supported.",
                                                    bindingType));
            }
        }
Example #3
0
        public void Bind <TSource>(INotifyingObject <TSource> source, OneWayToSourceValueConverter <TSource, T> converter)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }
            if (Bound)
            {
                throw new InvalidOperationException(AlreadyBoundMessage);
            }

            _binding         = _bindingFactory.CreatePropertyBinding(BindingType.OneWayToSource, this, source, converter);
            _binding.Culture = _culture;
        }
Example #4
0
        public IBinding CreatePropertyBinding <TSource, TTarget>(
            BindingType bindingType,
            IDependencyProperty <TTarget> target,
            INotifyingObject <TSource> source,
            ValueConverter <TSource, TTarget> converter)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            return(new PropertyBinding <TSource, TTarget>(bindingType, target, source, converter));
        }
Example #5
0
        public BasePropertyValueModel(INotifyingObject obj, Property prop)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (prop == null)
            {
                throw new ArgumentNullException("prop");
            }

            this.Property = prop;
            this.Object   = obj;

            this.Object.PropertyChanged += Object_PropertyChanged;
            if (this.Object is IPersistenceObject)
            {
                DataContext = ((IPersistenceObject)this.Object).Context;
                DataContext.IsElevatedModeChanged += new EventHandler(Context_IsElevatedModeChanged);
            }
        }
Example #6
0
        public static IValueModel GetPropertyValueModel(this Property prop, INotifyingObject obj)
        {
            if (prop == null)
            {
                throw new ArgumentNullException("prop");
            }
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (prop is IntProperty)
            {
                return(new NullableStructPropertyValueModel <int>(obj, prop));
            }
            else if (prop is BoolProperty)
            {
                return(new BoolPropertyValueModel(obj, (BoolProperty)prop));
            }
            else if (prop is DoubleProperty)
            {
                return(new NullableStructPropertyValueModel <double>(obj, prop));
            }
            else if (prop is DecimalProperty)
            {
                return(new DecimalPropertyValueModel(obj, (DecimalProperty)prop));
            }
            else if (prop is GuidProperty)
            {
                return(new NullableStructPropertyValueModel <Guid>(obj, prop));
            }
            else if (prop is DateTimeProperty)
            {
                return(new DateTimePropertyValueModel(obj, (DateTimeProperty)prop));
            }
            else if (prop is EnumerationProperty)
            {
                return(new EnumerationPropertyValueModel(obj, (EnumerationProperty)prop));
            }
            else if (prop is StringProperty)
            {
                return(new ClassPropertyValueModel <string>(obj, prop));
            }
            else if (prop is ObjectReferenceProperty)
            {
                var objRefProp = (ObjectReferenceProperty)prop;
                if (objRefProp.GetIsList())
                {
                    var sorted = objRefProp.RelationEnd.Parent.GetOtherEnd(objRefProp.RelationEnd).HasPersistentOrder;
                    if (sorted)
                    {
                        return(new ObjectListPropertyValueModel(obj, objRefProp));
                    }
                    else
                    {
                        return(new ObjectCollectionPropertyValueModel(obj, objRefProp));
                    }
                }
                else
                {
                    return(new ObjectReferencePropertyValueModel(obj, objRefProp));
                }
            }
            else if (prop is CalculatedObjectReferenceProperty)
            {
                var objRefProp = (CalculatedObjectReferenceProperty)prop;
                return(new CalculatedObjectReferencePropertyValueModel(obj, objRefProp));
            }
            else if (prop is CompoundObjectProperty)
            {
                var cop = (CompoundObjectProperty)prop;
                if (cop.IsList)
                {
                    return(new CompoundCollectionPropertyValueModel(obj, cop));
                }
                else
                {
                    return(new CompoundObjectPropertyValueModel(obj, cop));
                }
            }
            else
            {
                throw new NotImplementedException(string.Format("GetValueModel is not implemented for {0} properties yet", prop.GetPropertyTypeString()));
            }
        }
Example #7
0
 public ObjectCollectionPropertyValueModel(INotifyingObject obj, ObjectReferenceProperty prop)
     : base(obj, prop)
 {
 }
Example #8
0
 public BaseObjectCollectionPropertyValueModel(INotifyingObject obj, ObjectReferenceProperty prop)
     : base(obj, prop)
 {
     this.objRefProp = prop;
 }
Example #9
0
 public CompoundCollectionPropertyValueModel(INotifyingObject obj, CompoundObjectProperty prop)
     : base(obj, prop)
 {
     _property = prop;
 }
Example #10
0
 public CompoundObjectPropertyValueModel(INotifyingObject obj, CompoundObjectProperty prop)
     : base(obj, prop)
 {
     this.cProp = prop;
 }
Example #11
0
 public CalculatedObjectReferencePropertyValueModel(INotifyingObject obj, CalculatedObjectReferenceProperty prop)
     : base(obj, prop)
 {
     this.objRefProp = prop;
 }
Example #12
0
 public ClassPropertyValueModel(INotifyingObject obj, Property prop)
     : base(obj, prop)
 {
 }
Example #13
0
        public void HandleWhenChanged <TProperty>(ChangedEventHandler <TProperty> propertyChangedHandler, int propertyPathPos, params string[] propertyPath)
        {
            int    pathCount = propertyPath.Length - propertyPathPos;
            string currentpathPropertyName = propertyPath[propertyPathPos];

            if (pathCount > 1)
            {
                var  propertyInfo         = this.GetType().GetProperty(currentpathPropertyName);
                var  propertyType         = propertyInfo.PropertyType;
                Type iNotifyingObjectType = typeof(INotifyingObject);
                //var notifyingObjectType = typeof(NotifyingObject<>);
                if (iNotifyingObjectType.IsAssignableFrom(propertyType))
                {
                    INotifyingObject childNotifyingObject = (INotifyingObject)propertyInfo.GetValue(this, null);
                    if (childNotifyingObject != null)
                    {
                        childNotifyingObject.HandleWhenChanged(propertyChangedHandler, propertyPathPos + 1, propertyPath);
                    }
                    else
                    {
                        PropertyEventsPath currentPropertyEventsPath;
                        if (_propertiesEvents == null)
                        {
                            _propertiesEvents         = new Dictionary <string, PropertyEventsPath>();
                            currentPropertyEventsPath = new PropertyEventsPath();
                            _propertiesEvents.Add(currentpathPropertyName, currentPropertyEventsPath);
                        }
                        else
                        {
                            currentPropertyEventsPath = _propertiesEvents.GetValueOrAddIfNotExists(currentpathPropertyName, () => new PropertyEventsPath());
                        }
                        for (int pathPos = propertyPathPos, i = 1; i < pathCount; pathPos++, i++)
                        {
                            PropertyEventsPath child;
                            if (currentPropertyEventsPath.PropertiesEvents == null)
                            {
                                currentPropertyEventsPath.PropertiesEvents = new Dictionary <string, PropertyEventsPath>();
                                child = new PropertyEventsPath();
                                currentPropertyEventsPath.PropertiesEvents.Add(propertyPath[pathPos], child);
                            }
                            else
                            {
                                child = currentPropertyEventsPath.PropertiesEvents.GetValueOrAddIfNotExists(propertyPath[pathPos], () => new PropertyEventsPath());
                            }
                            currentPropertyEventsPath = child;
                        }
                        if (currentPropertyEventsPath.Events == null)
                        {
                            currentPropertyEventsPath.Events = new List <Delegate>();
                        }
                        currentPropertyEventsPath.Events.Add(propertyChangedHandler);
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format(
                                                            "You can't specify a property that has a parent type which is not derived from NotifyingObject<{0}>. "
                                                            + "Details: The type \"{0}\" is not derived from NotifyingObject<{0}>", propertyType.FullName));
                }
            }
            else
            {
                PropertyEventsPath currentPropertyEventsPath;
                if (_propertiesEvents == null)
                {
                    _propertiesEvents                = new Dictionary <string, PropertyEventsPath>();
                    currentPropertyEventsPath        = new PropertyEventsPath();
                    currentPropertyEventsPath.Events = new List <Delegate>();
                    _propertiesEvents.Add(currentpathPropertyName, currentPropertyEventsPath);
                }
                else
                {
                    currentPropertyEventsPath = _propertiesEvents.GetValueOrAddIfNotExists(currentpathPropertyName, () => new PropertyEventsPath());
                    if (currentPropertyEventsPath.Events == null)
                    {
                        currentPropertyEventsPath.Events = new List <Delegate>();
                    }
                }
                currentPropertyEventsPath.Events.Add(propertyChangedHandler);
            }
        }
Example #14
0
 public DecimalPropertyValueModel(INotifyingObject obj, DecimalProperty prop)
     : base(obj, prop)
 {
     dProp = prop;
 }
Example #15
0
 public BoolPropertyValueModel(INotifyingObject obj, BoolProperty prop)
     : base(obj, prop)
 {
     bProp = prop;
 }
Example #16
0
 public DateTimePropertyValueModel(INotifyingObject obj, DateTimeProperty prop)
     : base(obj, prop)
 {
     dtProp = prop;
 }
Example #17
0
 public EnumerationPropertyValueModel(INotifyingObject obj, EnumerationProperty prop)
     : base(obj, prop)
 {
     enumProp = prop;
 }
Example #18
0
 public NullableStructPropertyValueModel(INotifyingObject obj, Property prop)
     : base(obj, prop)
 {
 }
Example #19
0
        /// <summary>
        /// This method must be used in derived objects next way: set { SetNotifyingProperty(() => PropertyName, ref fieldForPropertyName, value); }
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expressionWithPropertyName"></param>
        /// <param name="fieldForPropertyName"></param>
        /// <param name="value"></param>
        protected void SetNotifyingProperty <T>(Expression <Func <T> > expressionWithPropertyName, ref T fieldForPropertyName, T value)
        {
            if (!fieldForPropertyName.EqualsWithNullHandling(value))
            {
                T oldValue = fieldForPropertyName;
                fieldForPropertyName = value;
                string             propertyName;
                PropertyEventsPath currentPropertyEventsPath;
                if (_propertiesEvents != null)
                {
                    propertyName = expressionWithPropertyName.GetBodyMemberName();
                    if (_propertiesEvents.TryGetValue(propertyName, out currentPropertyEventsPath))
                    {
                        if (oldValue == null)
                        {
                            if (currentPropertyEventsPath.PropertiesEvents != null)
                            {
                                try
                                {
                                    INotifyingObject newValNotifyingObj = (INotifyingObject)value;
                                    newValNotifyingObj.SetPropertiesEvents(MergePropertyEventsPaths(newValNotifyingObj.GetPropertiesEvents(), currentPropertyEventsPath.PropertiesEvents));
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new InvalidOperationException(string.Format("Early handlers were subscribed to child properties: \"{0}\" "
                                                                                      + "but it's a violating operation because  a parent property \"{1}\" doesn't implement \"{3}\"",
                                                                                      string.Join(", ", currentPropertyEventsPath.PropertiesEvents.Select(pair => string.Format("{0}: {1}", pair.Key, pair.Value)).ToArray()),
                                                                                      propertyName, typeof(INotifyingObject), ex));
                                }
                                currentPropertyEventsPath.PropertiesEvents = null;
                                if (currentPropertyEventsPath.Events == null)
                                {
                                    _propertiesEvents.Remove(propertyName);
                                }
                            }
                        }

                        if (currentPropertyEventsPath.Events != null)
                        {
                            ChangedEventArgs <T> e = new ChangedEventArgs <T>(oldValue, value);
                            foreach (ChangedEventHandler <T> changedEventHandler in currentPropertyEventsPath.Events)
                            {
                                changedEventHandler(this, e);
                            }
                            return;
                        }
                    }
                }
                else
                {
                    currentPropertyEventsPath = null;
                    propertyName = null;
                }

                INotifyingObject oldValNotifyingObj = oldValue as INotifyingObject;
                if (oldValNotifyingObj != null)
                {
                    oldValNotifyingObj.ChildrenChanged -= OnChildrenChanged;
                }
                if (value != null)
                {
                    INotifyingObject newValNotifyingObj = value as INotifyingObject;
                    if (newValNotifyingObj != null)
                    {
                        newValNotifyingObj.ChildrenChanged += OnChildrenChanged;
                    }
                }
                else
                {
                    var oldValNotifyingObjPropertiesEvents = oldValNotifyingObj.GetPropertiesEvents();
                    if (oldValNotifyingObjPropertiesEvents != null)
                    {
                        if (currentPropertyEventsPath == null)
                        {
                            currentPropertyEventsPath = new PropertyEventsPath {
                                PropertiesEvents = oldValNotifyingObjPropertiesEvents
                            };
                            if (propertyName == null)
                            {
                                propertyName = expressionWithPropertyName.GetBodyMemberName();
                            }
                            _propertiesEvents.Add(propertyName, currentPropertyEventsPath);
                        }
                        else
                        {
                            currentPropertyEventsPath.PropertiesEvents = MergePropertyEventsPaths(currentPropertyEventsPath.PropertiesEvents, oldValNotifyingObjPropertiesEvents);
                        }
                    }
                }
                OnChildrenChanged(this, null);
            }
        }