// Returns true if the value should be serialized to code.
        public override bool ShouldSerializeValue(object component)
        {
            // If the local value is set, see if the property supports
            // a ShouldSerialize on its property descriptor.  If it doesn't,
            // then we let the IsSet dictate the 'setness'.

            if (this.itemProperty.IsSet)
            {
                ModelPropertyImpl modelProp = this.itemProperty as ModelPropertyImpl;
                if (modelProp != null)
                {
                    return(modelProp.PropertyDescriptor.ShouldSerializeValue(this.itemProperty.Parent.GetCurrentValue()));
                }
                return(true);
            }

            return(false);
        }
        public override bool Apply()
        {
            Fx.Assert(this.ModelTreeManager != null, "Modeltreemanager cannot be null");
            Fx.Assert(this.Owner != null, "Owner modelitem cannot be null");
            Fx.Assert(!String.IsNullOrEmpty(this.PropertyName), " property name cannot be null or emptry");
            ModelPropertyImpl dataModelProperty = (ModelPropertyImpl)this.Owner.Properties[this.PropertyName];
            ModelItem         oldValue          = dataModelProperty.Value;

            if ((oldValue == null && this.NewValue == null) ||
                (oldValue != null && this.NewValue != null && oldValue.GetCurrentValue().Equals(this.NewValue.GetCurrentValue())))
            {
                return(false);
            }
            dataModelProperty.SetValueCore(this.NewValue);
            ModelChangeInfo changeInfo = ModelChangeInfoImpl.CreatePropertyChanged(this.Owner, this.PropertyName, this.OldValue, this.NewValue);

            this.ModelTreeManager.NotifyPropertyChange(dataModelProperty, changeInfo);
            return(true);
        }
Esempio n. 3
0
        private void EditCore(ModelItem key, ModelItem value, bool updateInstance)
        {
            try
            {
                ModelItem oldValue = this.modelItems[key];
                this.EditInProgress = true;
                Fx.Assert(this.instance != null, "instance should not be null");

                bool wasValueInKeysOrValuesCollection = this.IsInKeysOrValuesCollection(value);

                if (updateInstance)
                {
                    this.instance[(key == null) ? null : key.GetCurrentValue()] = null != value?value.GetCurrentValue() : null;

                    //this also makes sure ItemsCollectionModelItemCollection is not null
                    if (ItemsCollectionObject != null)
                    {
                        try
                        {
                            ItemsCollectionObject.ShouldUpdateDictionary = false;

                            foreach (ModelItem item in ItemsCollectionModelItemCollection)
                            {
                                ModelItem keyInCollection = item.Properties["Key"].Value;
                                bool      found           = (key == keyInCollection);

                                if (!found && key != null && keyInCollection != null)
                                {
                                    object keyValue = key.GetCurrentValue();

                                    // ValueType do not share ModelItem, a ModelItem is always created for a ValueType
                                    // ModelTreeManager always create a ModelItem even for the same string
                                    // So, we compare object instance instead of ModelItem for above cases.
                                    if (keyValue is ValueType || keyValue is string)
                                    {
                                        found = keyValue.Equals(keyInCollection.GetCurrentValue());
                                    }
                                }

                                if (found)
                                {
                                    ModelPropertyImpl valueImpl = item.Properties["Value"] as ModelPropertyImpl;
                                    if (valueImpl != null)
                                    {
                                        valueImpl.SetValueCore(value);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            ItemsCollectionObject.ShouldUpdateDictionary = true;
                        }
                    }
                }

                this.modelItems[key] = null;
                if (oldValue != null && !this.IsInKeysOrValuesCollection(oldValue))
                {
                    this.modelTreeManager.OnItemEdgeRemoved(this, oldValue);
                }

                this.modelItems[key] = value;
                if (value != null && !wasValueInKeysOrValuesCollection)
                {
                    this.modelTreeManager.OnItemEdgeAdded(this, value);
                }

                if (null != this.CollectionChanged)
                {
                    this.CollectionChanged(this,
                                           new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,
                                                                                new KeyValuePair <ModelItem, ModelItem>(key, value),
                                                                                new KeyValuePair <ModelItem, ModelItem>(key, oldValue)));
                }
            }
            finally
            {
                this.EditInProgress = false;
            }
        }