1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
8 using System.ComponentModel;
10 // This is a property descriptor that wraps ModelProperty objects.
11 // It is used when someone uses TypeDescriptor to ask for type information
12 // on an editing model item.
13 internal class ModelPropertyDescriptor : PropertyDescriptor
15 ModelProperty itemProperty;
16 TypeConverter converter;
18 internal ModelPropertyDescriptor(ModelProperty itemProperty)
19 : base(itemProperty.Name, null)
21 this.itemProperty = itemProperty;
24 public override AttributeCollection Attributes
26 get { return this.itemProperty.Attributes; }
29 // Returns the type converter for this property. Our property
30 // descriptor wrapper is "complete" in that it always returns
31 // editing model item objects. Because of that, we must wrap
32 // all type converters.
33 public override TypeConverter Converter
37 if (this.converter == null)
39 TypeConverter baseConverter = base.Converter;
40 IModelTreeItem propertyParent = this.itemProperty.Parent as IModelTreeItem;
41 this.converter = new ModelTypeConverter(propertyParent.ModelTreeManager, baseConverter);
49 public override bool IsBrowsable
51 get { return this.itemProperty.IsBrowsable; }
54 // Returns the type of object that defined this property.
55 public override Type ComponentType
57 get { return this.itemProperty.Parent.ItemType; }
60 public override bool IsReadOnly
62 get { return this.itemProperty.IsReadOnly; }
65 // Returns the data type of the property.
66 public override Type PropertyType
71 return this.itemProperty.PropertyType;
76 public override PropertyDescriptorCollection GetChildProperties(object instance, Attribute[] filter)
78 return base.GetChildProperties(instance, filter);
81 public override bool CanResetValue(object component)
83 return this.itemProperty.IsSet;
86 public override object GetEditor(Type editorBaseType)
88 // The new PropertyEntry PropertyValue editor model does not use this
92 // Returns the current value of this property.
93 // When the object is not primitive, enum, or string we always return the ModelItem wrapping it.
94 // this enables nested binding in wpf still go through modelItems tree so taht we can intercept
95 // the property sets e.g "{Binding Path=RootModel.ComplexProperty.Blah"}, since we return a ModelItem
96 // for ComplexProperty we can still intercept sets made from Wpf controls to Blah even if ComplexProperty
97 // type does not implement INotifyPropertyChanged.
98 public override object GetValue(object component)
100 ModelItem value = this.itemProperty.Value;
105 Type itemType = value.ItemType;
106 if (itemType.IsPrimitive || itemType.IsEnum || itemType.Equals(typeof(String)))
108 return value.GetCurrentValue();
113 public override void ResetValue(object component)
115 this.itemProperty.ClearValue();
118 // Sets the property value to the given value. For
119 // convenience, the value passed can either be an
120 // item or a raw value. In the latter case we will
121 // wrap the value into an item for you.
122 public override void SetValue(object component, object value)
124 this.itemProperty.SetValue(value);
127 // Returns true if the value should be serialized to code.
128 public override bool ShouldSerializeValue(object component)
130 // If the local value is set, see if the property supports
131 // a ShouldSerialize on its property descriptor. If it doesn't,
132 // then we let the IsSet dictate the 'setness'.
134 if (this.itemProperty.IsSet)
136 ModelPropertyImpl modelProp = this.itemProperty as ModelPropertyImpl;
137 if (modelProp != null)
139 return modelProp.PropertyDescriptor.ShouldSerializeValue(this.itemProperty.Parent.GetCurrentValue());