/// <summary> /// This method is called when a contextual menu is requested for a list box item. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event args.</param> private void OnToolboxItemMenuRequested(object sender, TreeListBoxItemMenuEventArgs e) { // Add model-specific menu items if (e.Item is ControlTreeNodeModel controlModel) { StartNewMenuGroup(); e.Menu.Items.Add(new MenuItem() { Header = controlModel.IsFavorite ? "Remove from Favorites" : "Add to Favorites", Command = controlModel.ToggleFavoriteCommand }); } else if (e.Item is FavoritesCategoryTreeNodeModel) { StartNewMenuGroup(); e.Menu.Items.Add(new MenuItem() { Header = "Clear Favorites", Command = new DelegateCommand <object>(p => ControlDataRepository.Instance.ClearFavorites()) }); } // Add common menu items StartNewMenuGroup(); e.Menu.Items.Add(new MenuItem() { Header = "Reset Toolbox", Command = new DelegateCommand <object>(p => ResetToolbox()) }); // Call this method to prepare a menu for a new group of menu items void StartNewMenuGroup() { if (e.Menu is null) { e.Menu = new ContextMenu(); } else { e.Menu.Items.Add(new Separator()); } } }
/// <summary> /// Occurs when an item requests a context menu. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">The <c>TreeListBoxItemMenuEventArgs</c> that contains the event data.</param> private void OnPropertyGridItemMenuRequested(object sender, TreeListBoxItemMenuEventArgs e) { var propertyModel = e.Item as IPropertyModel; if (propertyModel != null) { if (propertyModel.ValueType == typeof(Color)) { if (e.Menu == null) { e.Menu = new ContextMenu(); } else { e.Menu.Items.Add(new Separator()); } var yellowMenuItem = new MenuItem(); yellowMenuItem.Header = "Set Color to Yellow (custom menu item)"; yellowMenuItem.Command = new DelegateCommand <IPropertyModel>(SetColorToYellow); yellowMenuItem.CommandParameter = propertyModel; e.Menu.Items.Add(yellowMenuItem); } else if (propertyModel.ValueType == typeof(string)) { if (e.Menu == null) { e.Menu = new ContextMenu(); } else { e.Menu.Items.Add(new Separator()); } var appendMenuItem = new MenuItem(); appendMenuItem.Header = "Append 'Foo' Text (custom menu item)"; appendMenuItem.Command = new DelegateCommand <IPropertyModel>(AppendText); appendMenuItem.CommandParameter = propertyModel; e.Menu.Items.Add(appendMenuItem); } } else { var categoryModel = e.Item as ICategoryModel; if (categoryModel != null) { if (e.Menu == null) { e.Menu = new ContextMenu(); } else { e.Menu.Items.Add(new Separator()); } var toggleMenuItem = new MenuItem(); toggleMenuItem.Header = "Toggle Expansion (custom menu item)"; toggleMenuItem.Command = new DelegateCommand <IDataModel>(ToggleExpansion); toggleMenuItem.CommandParameter = categoryModel; e.Menu.Items.Add(toggleMenuItem); } } }