public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { PropertiesContainer obj = (value as PropertiesContainer); if (obj == null) { return(new PropertyDescriptorCollection(new PropertyDescriptor[] {})); } List <Property> propertyList = obj.PropertyList; PropertyDescriptor[] props = new PropertyDescriptor[propertyList.Count]; // Create the list of property descriptors. for (int i = 0; i < propertyList.Count; i++) { Property property = propertyList[i]; string name = property.Name; UITypeEditor editor = null; PropertyDocumentation documentation = property.GetDocumentation(); // Find the editor. if (documentation != null) { editor = obj.PropertyGrid.GetUITypeEditor(documentation.EditorType); } // Create the property descriptor. props[i] = new CustomPropertyDescriptor( documentation, editor, property.Name, obj.Properties); } return(new PropertyDescriptorCollection(props)); }
//----------------------------------------------------------------------------- // Events //----------------------------------------------------------------------------- private void OnPropertyChange(object sender, PropertyValueChangedEventArgs e) { CustomPropertyDescriptor propertyDescriptor = e.ChangedItem.PropertyDescriptor as CustomPropertyDescriptor; Property property = propertyDescriptor.Property; PropertyDocumentation propertyDoc = property.GetRootDocumentation(); // Handle special property editor-types. if (propertyDoc != null && propertyDoc.EditorType == "script") { string oldValue = e.OldValue.ToString(); string newValue = e.ChangedItem.Value.ToString(); Script oldScript = editorControl.World.GetScript(oldValue); Script newScript = editorControl.World.GetScript(newValue); bool isNewScriptInvalid = (newScript == null && newValue.Length > 0); // When a script property is changed from a hidden script to something else. if (oldScript != null && oldScript.IsHidden && newScript != oldScript) { // Delete the old script from the world (because it is now unreferenced). editorControl.World.RemoveScript(oldScript); Console.WriteLine("Deleted unreferenced script '" + oldValue + "'"); // Don't allow the user to reference other hidden scripts. if (newScript != null && newScript.IsHidden) { isNewScriptInvalid = true; } } // Show a message if the script is invalid. if (isNewScriptInvalid) { MessageBox.Show("'" + newValue + "' is not a valid script name."); } } }