Skip to main content

How to: Add new menu item to the Layout Tree View Context Menu

  • 2 minutes to read

This example handles the LayoutControl.LayoutTreeViewPopupMenuShowing event to add a command to the Layout Tree View Context Menu to toggle the Bold font attribute of the item text.

tbhTreeViewMenu

using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.XtraLayout;
// ...
private void layoutControl1_LayoutTreeViewPopupMenuShowing(object sender, DevExpress.XtraLayout.PopupMenuShowingEventArgs e) {
    BaseLayoutItem layoutItem = e.HitInfo.Item as BaseLayoutItem;
    if (layoutItem == null) return;
    bool isTextBold = layoutItem.AppearanceItemCaption.Font.Bold;
    DXMenuCheckItem menuItem = new DXMenuCheckItem("Bold Text", isTextBold, null, new EventHandler(this.ToggleBoldTextMenuItemClick));
    menuItem.Image = DxImageAssemblyUtil.ImageProvider.GetImage("Bold", ImageSize.Size16x16, ImageType.Colored);
    menuItem.Tag = layoutItem;
    //Add a separator
    e.Menu.Items.Add(new DXMenuItem("-"));
    //Add the "Bold Text" check item.
    e.Menu.Items.Add(menuItem);
}

private void ToggleBoldTextMenuItemClick(object sender, EventArgs e) {
    DXMenuItem menuItem = sender as DXMenuItem;
    BaseLayoutItem layoutItem = menuItem.Tag as BaseLayoutItem;
    if (layoutItem == null) return;
    bool isTextBold = layoutItem.AppearanceItemCaption.Font.Bold;
    Font newFont;
    if (isTextBold)
        newFont = new Font(layoutItem.AppearanceItemCaption.Font, FontStyle.Regular);
    else
        newFont = new Font(layoutItem.AppearanceItemCaption.Font, FontStyle.Bold);
    layoutItem.AppearanceItemCaption.Font = newFont;
}