コード例 #1
0
        /// <summary>
        /// Edits the value of the specified object using the editor style indicated by <seealso cref="UITypeEditor.GetEditStyle(ITypeDescriptorContext)"/>.
        /// </summary>
        /// <param name="context">An <see cref="ITypeDescriptorContext"/> that can be used to gain additional context information. </param>
        /// <param name="provider">An <see cref="IServiceProvider"/> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>The new value of the object.</returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (null == context)
            {
                return(null);
            }
            if (null == provider)
            {
                return(null);
            }

            IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (service != null)
            {
                ReferenceTypeAttribute       refTypeAttribute = GetReferenceType(context);
                ConfigurationNode            currentNode      = (ConfigurationNode)context.Instance;
                ConfigurationApplicationNode appNode          = GetApplicationNode(currentNode);
                ConfigurationNode            contextNode      = (refTypeAttribute.LocalOnly) ? currentNode : appNode;
                ReferenceEditorUI            control          = new ReferenceEditorUI(contextNode, refTypeAttribute.ReferenceType, (ConfigurationNode)value, service, IsRequired(context));
                service.DropDownControl(control);
                if (control.SelectedNode != null)
                {
                    Type propertyType     = context.PropertyDescriptor.PropertyType;
                    Type selectedNodeType = control.SelectedNode.GetType();
                    if (propertyType == selectedNodeType || selectedNodeType.IsSubclassOf(propertyType))
                    {
                        return(control.SelectedNode);
                    }
                }
            }

            return(null);
        }
コード例 #2
0
        private static ReferenceTypeAttribute GetReferenceType(ITypeDescriptorContext context)
        {
            foreach (Attribute attribute in context.PropertyDescriptor.Attributes)
            {
                ReferenceTypeAttribute refAttribute = attribute as ReferenceTypeAttribute;
                if (null != refAttribute)
                {
                    return(refAttribute);
                }
            }

            throw new InvalidOperationException(Resources.ExceptionNoRefTypeAttribute);
        }