1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
7 using System.Collections.Generic;
8 using System.Activities.Presentation.View;
9 using System.ComponentModel;
14 // This provides a container for model properties of a modelItem.
15 // This uses the TypeDescriptor.GetProperties() instead of using reflection
16 // to get the properties of a model item. So any model instance implementing ICustomTypeProvider
17 // is automatically taken care of.
19 class ModelPropertyCollectionImpl : ModelPropertyCollection
22 bool createFakeModelProperties;
24 public ModelPropertyCollectionImpl(ModelItem parent)
27 createFakeModelProperties = this.parent is FakeModelItemImpl;
30 public override IEnumerator<ModelProperty> GetEnumerator()
32 foreach (PropertyDescriptor propertyDescriptor in GetPropertyDescriptors())
34 yield return CreateProperty(parent, propertyDescriptor);
38 protected override ModelProperty Find(System.Windows.DependencyProperty value, bool throwOnError)
40 // We dont support dependency properties.
43 throw FxTrace.Exception.AsError(new NotSupportedException());
51 protected override ModelProperty Find(string name, bool throwOnError)
53 PropertyDescriptor propertyDescriptor = GetPropertyDescriptors()[name];
54 if (propertyDescriptor != null)
56 return CreateProperty(parent, propertyDescriptor);
61 ModelProperty CreateProperty(ModelItem parent, PropertyDescriptor propertyDescriptor)
63 bool isAttached = propertyDescriptor is AttachedPropertyDescriptor;
64 return this.createFakeModelProperties ?
65 (ModelProperty)(new FakeModelPropertyImpl((FakeModelItemImpl)parent, propertyDescriptor)) :
66 (ModelProperty)(new ModelPropertyImpl(parent, propertyDescriptor, isAttached));
69 PropertyDescriptorCollection GetPropertyDescriptors()
71 PropertyDescriptorCollection propertyDescriptors = PropertyDescriptorCollection.Empty;
75 object instance = parent.GetCurrentValue();
78 if (!(instance is ICustomTypeDescriptor))
80 Type instanceType = instance.GetType();
81 if (instanceType.IsValueType)
83 propertyDescriptors = TypeDescriptor.GetProvider(instanceType).GetTypeDescriptor(instanceType).GetProperties();
87 propertyDescriptors = TypeDescriptor.GetProvider(instance).GetTypeDescriptor(instance).GetProperties();
92 propertyDescriptors = TypeDescriptor.GetProperties(instance);
97 // Add browsable attached properties
98 AttachedPropertiesService AttachedPropertiesService = this.parent.GetEditingContext().Services.GetService<AttachedPropertiesService>();
99 if (AttachedPropertiesService != null)
102 var browsableAttachedProperties = from attachedProperty in AttachedPropertiesService.GetAttachedProperties(this.parent.ItemType)
103 where (attachedProperty.IsBrowsable || attachedProperty.IsVisibleToModelItem)
104 select new AttachedPropertyDescriptor(attachedProperty, this.parent);
106 List<PropertyDescriptor> mergedProperties = new List<PropertyDescriptor>();
107 foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors)
109 mergedProperties.Add(propertyDescriptor);
111 propertyDescriptors = new PropertyDescriptorCollection(mergedProperties.Concat(browsableAttachedProperties).ToArray(), true);
115 catch (FileNotFoundException e)
117 EditingContext context = parent.GetEditingContext();
118 if (context.Items.GetValue<ErrorItem>() == null)
120 context.Items.SetValue(new ErrorItem { Message = e.Message, Details = e.ToString() });
124 return propertyDescriptors;