1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Windows.Documents;
11 // This class provides the implementation for a model property.
12 // this intercepts sets /gets to the property and works with modeltreemanager
13 // to keep the xaml in [....].
15 class ModelPropertyImpl : ModelProperty
19 PropertyDescriptor propertyDescriptor;
20 bool isAttached = false;
22 public ModelPropertyImpl(ModelItem parent, PropertyDescriptor propertyDescriptor, bool isAttached)
25 this.propertyDescriptor = propertyDescriptor;
27 // using this a marker to indicate this hasnt been computed yet.
28 this.defaultValue = this;
29 this.isAttached = isAttached;
32 public override Type AttachedOwnerType
36 return this.parent.GetType();
40 public override AttributeCollection Attributes
44 return this.propertyDescriptor.Attributes;
48 public override ModelItemCollection Collection
52 return this.Value as ModelItemCollection;
56 public override object ComputedValue
60 object computedValue = null;
61 if (this.Value != null)
63 computedValue = Value.GetCurrentValue();
73 public override System.ComponentModel.TypeConverter Converter
77 return new ModelTypeConverter(((IModelTreeItem)this.Parent).ModelTreeManager, propertyDescriptor.Converter);
81 public override object DefaultValue
85 if (Object.ReferenceEquals(this.defaultValue, this))
87 DefaultValueAttribute defaultValueAttribute = this.propertyDescriptor.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
88 this.defaultValue = (defaultValueAttribute == null) ? null : defaultValueAttribute.Value;
90 return this.defaultValue;
94 public override ModelItemDictionary Dictionary
98 return Value as ModelItemDictionary;
102 public override bool IsAttached
106 return this.isAttached;
110 public override bool IsBrowsable
114 return propertyDescriptor.IsBrowsable;
118 public override bool IsCollection
120 get { return this.Value is ModelItemCollection; }
123 public override bool IsDictionary
125 get { return this.Value is ModelItemDictionary; }
128 public override bool IsReadOnly
132 return propertyDescriptor.IsReadOnly;
136 public override bool IsSet
140 return this.Value != null;
144 public override string Name
148 return propertyDescriptor.Name;
152 public override ModelItem Parent
160 public override Type PropertyType
164 return propertyDescriptor.PropertyType;
168 public override ModelItem Value
172 return ((IModelTreeItem)parent).ModelTreeManager.GetValue(this);
176 internal PropertyDescriptor PropertyDescriptor
180 return propertyDescriptor;
184 public override void ClearValue()
186 if (!this.IsReadOnly)
188 ((IModelTreeItem)parent).ModelTreeManager.ClearValue(this);
192 public override ModelItem SetValue(object value)
194 return ((IModelTreeItem)parent).ModelTreeManager.SetValue(this, value);
197 internal override string Reference
201 return PropertyReferenceUtilities.GetPropertyReference(parent.GetCurrentValue(), this.Name);
205 internal override void ClearReference()
207 this.SetReference(null);
210 internal override void SetReference(string sourceProperty)
212 PropertyReferenceChange change = new PropertyReferenceChange()
215 TargetProperty = this.Name,
216 OldSourceProperty = this.Reference,
217 NewSourceProperty = sourceProperty
220 ((IModelTreeItem)parent).ModelTreeManager.AddToCurrentEditingScope(change);
223 internal object SetValueCore(ModelItem newValueModelItem)
225 object newValueInstance = (newValueModelItem == null) ? null : newValueModelItem.GetCurrentValue();
226 ModelItem oldValueModelItem = this.Value;
227 IModelTreeItem parent = (IModelTreeItem)this.Parent;
229 // update object instance
230 this.PropertyDescriptor.SetValue(this.Parent.GetCurrentValue(), newValueInstance);
232 if (oldValueModelItem != null && !this.isAttached)
234 parent.ModelPropertyStore.Remove(this.Name);
235 parent.ModelTreeManager.OnPropertyEdgeRemoved(this.Name, this.Parent, oldValueModelItem);
238 if (newValueModelItem != null && !this.isAttached)
240 parent.ModelPropertyStore.Add(this.Name, newValueModelItem);
241 parent.ModelTreeManager.OnPropertyEdgeAdded(this.Name, this.Parent, newValueModelItem);
245 ((IModelTreeItem)this.Parent).OnPropertyChanged(this.Name);
246 return newValueInstance;