/// <summary>
    /// Initializes a new instance of the <see cref="PropertyItemValue"/> class.
    /// </summary>
    /// <param name="property">The property.</param>
    public PropertyItemValue(PropertyItem property)      
    {
      if (property == null) throw new ArgumentNullException("property");
      this._property = property;

      var context = new TypeDescriptorContext(property);
      _hasSubProperties = property.Converter.GetPropertiesSupported(context);

      if (_hasSubProperties)
      {
        object value = property.GetValue();

        if(value == null)
            return;

        PropertyDescriptorCollection descriptors = property.Converter.GetProperties(context, value); 
        foreach (PropertyDescriptor d in descriptors)
        {
          _subProperties.Add(new PropertyItem(property.Owner, value, d));  
          // TODO: Move to PropertyData as a public property
          NotifyParentPropertyAttribute notifyParent = d.Attributes[KnownTypes.Attributes.NotifyParentPropertyAttribute] as NotifyParentPropertyAttribute;
          if (notifyParent != null && notifyParent.NotifyParent)
          {
            d.AddValueChanged(value, NotifySubPropertyChanged);
          }
        }
      }
                        
      this._property.PropertyChanged += new PropertyChangedEventHandler(ParentPropertyChanged);      
    }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the editor.
        /// </summary>
        /// <param name="propertyItem">The property item.</param>
        /// <returns>Editor for Property</returns>
        public Editor GetEditor(PropertyItem propertyItem)
        {
            if (propertyItem == null)
            {
                throw new ArgumentNullException("propertyItem");
            }

            Editor editor;

            if (propertyItem.Attributes != null)
            {
                editor = GetPropertyEditorByAttributes(propertyItem.Attributes);
                if (editor != null)
                {
                    return(editor);
                }
            }

            if (propertyItem.Component != null && !string.IsNullOrEmpty(propertyItem.Name))
            {
                object declaringObject = ObjectServices.GetUnwrappedObject(propertyItem.Owner.SelectedObject);
                editor = FindPropertyEditor(declaringObject.GetType(), propertyItem.Name);
                if (editor != null)
                {
                    return(editor);
                }
            }

            bool hasType = propertyItem.PropertyType != null;

            var context = new TypeDescriptorContext(propertyItem);
            var hasExclusiveStandardValues = propertyItem.Converter.GetStandardValuesExclusive(context);

            if (hasType)
            {
                editor = FindTypeEditor(propertyItem.PropertyType);
                if (editor != null)
                {
                    return(editor);
                }
            }

            if (propertyItem.PropertyValue.HasSubProperties)
            {
                if (hasExclusiveStandardValues)
                {
                    var standardValues = propertyItem.Converter.GetStandardValues(context);
                    return(new TypeEditor(propertyItem.PropertyType, EditorKeys.ComplexPropertyEnumEditorKey));
                }

                return(new TypeEditor(propertyItem.PropertyType, EditorKeys.ComplexPropertyEditorKey));
            }

            if (hasType)
            {
                foreach (var cachedEditor in Cache)
                {
                    if (cachedEditor.Key.IsAssignableFrom(propertyItem.PropertyType))
                    {
                        return(cachedEditor.Value);
                    }
                }

                if (hasExclusiveStandardValues)
                {
                    try
                    {
                        var standardValues = propertyItem.Converter.GetStandardValues(context);
                        return(new TypeEditor(propertyItem.PropertyType, EditorKeys.EnumEditorKey));
                    }
                    catch (NotImplementedException)
                    {
                        /* failed to get standardValues. Not an Enum? */
                    }
                }

                return(new TypeEditor(propertyItem.PropertyType, EditorKeys.DefaultEditorKey));
            }

            return(null);
        }