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); } } } }