/// <summary> /// Update the Grid with a new target /// </summary> /// <param name="oldObject">the previous object edited</param> /// <param name="newObject">the new object being edited</param> private void UpdatePropertyGridTarget(object oldObject, object newObject) { Cleanup(); ClearGridUI(); ResetCategories(); if (newObject != null) { componentMode = newObject is IComponentContainer; //ComponentContainers and PropertyProviders are handled differently with regards to //how they are categorizer and put into category containers //as well as the possible header of the category container if (componentMode) { IComponentContainer componentContainer = newObject as IComponentContainer; foreach (IInspectableComponent component in componentContainer.GetInspectableComponents()) { AddInspectableComponent(component); } //Listen for component added/removed events componentContainer.ComponentAdded += ComponentContainer_ComponentAdded; //componentContainer.ComponentRemoved //Add the button at the bottom to pick components AddBehaviourButton(); } else { foreach (InspectableProperty property in DefaultPropertyFactory.GetProperties(newObject)) { InspectablePropertyMetadata propertyMetadata = DefaultPropertyFactory.GetPropertyMetadata(property); CategoryContainer container = GetDefaultHeaderCategoryContainer(propertyMetadata.Category); ListenToPropertyChanged(property); AddProperty(property, propertyMetadata, container); } } } else { SetEmptyGridUi(); } }
private void OpenContextMenu(object sender) { //Generate the available ones based on the selected object IComponentContainer target = SelectedObject as IComponentContainer; Button button = sender as Button; if (target != null && button != null) { ComponentDescriptor[] options = target.GetAvailableComponents(); IInspectableComponent[] behaviours = target.GetInspectableComponents(); ContextMenu menu = button.ContextMenu; menu.Items.Clear(); //Get them sorted by group var groupedOptions = options.GroupBy(item => item.Group, (key, group) => new { Group = key, Components = group }); foreach (var group in groupedOptions) { MenuItem menuItem = new MenuItem(); menuItem.Header = group.Group; menu.Items.Add(menuItem); foreach (var component in group.Components) { //Ignore the ones that cant be removed if (!component.Removable) { continue; } //Change the tooltip based on two cases //If the component already exists on the object and its unique, disable and show "Behaviour already added" //If the component is removable and not unique just show the description of it bool isEnabled = true; string tooltip = string.Empty; //Check if the behaviour already exists if (behaviours.Any(b => options.Any(o => b.GetType() == o.ComponentType)) && component.Unique) { isEnabled = false; tooltip = "Behaviour already added"; } else { tooltip = component.Description; } MenuItem child = new MenuItem(); child.Header = component.Title; child.IsEnabled = isEnabled; //Setup click handler if (isEnabled) { child.Click += (s, args) => { AddComponentToCurrentTarget(target, component); }; } menuItem.Items.Add(child); } } } }