C# .NET API · WPF Ribbon & PaletteSet

Programmatic Ribbon Tabs, Split Buttons & Dark-Theme Dockable PaletteSets in GstarCAD .NET (dev-cookbook-23)

Build modern WPF PaletteSets and dynamic Ribbon tabs in GstarCAD 2027 C# .NET using Gssoft.GstarCAD.Windows and ComponentManager with automatic Dark/Light theme synchronization.

1. Runtime Ribbon Injection vs. Static `.CUIX` Packaging

Enterprise `.NET` plugins deployed across GstarCAD 2027 workstations typically combine two UI surfaces: a top-level **Ribbon Tab** (`Gssoft.Windows.RibbonTab`) containing command buttons, split dropdown menus, and active layer/scale combo boxes, paired with a dockable **WPF `PaletteSet`** (`Gssoft.GstarCAD.Windows.PaletteSet`) for interactive data grids and BOM trees.

Constructing the Ribbon tab programmatically in C# via `ComponentManager.Ribbon` offers a major advantage over static `.cuix` files: your plugin can dynamically enable, disable, or hide specific Ribbon panels based on the logged-in user's Active Directory role or license tier without maintaining multiple `.cuix` variants.

2. C# .NET Implementation: Ribbon Tab + CommandHandler & Dockable WPF PaletteSet

When implementing `IExtensionApplication.Initialize()`, remember that `ComponentManager.Ribbon` may still be `null` during early startup before the main workspace finishes rendering. Always subscribe to `ComponentManager.ItemInitialized` or `Application.Idle` to inject your Ribbon tab safely:

using System;
using System.Windows.Input;
using Gssoft.GstarCAD.Runtime;
using Gssoft.GstarCAD.ApplicationServices;
using Gssoft.GstarCAD.Windows;
using Gssoft.Windows;

namespace GstarCadSamples.EnterpriseUI
{
    public class RibbonCommandRouter : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public bool CanExecute(object parameter) => true;

        public void Execute(object parameter)
        {
            if (parameter is RibbonCommandItem cmdItem && !string.IsNullOrEmpty(cmdItem.CommandParameter as string))
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                doc?.SendStringToExecute((string)cmdItem.CommandParameter, true, false, true);
            }
        }
    }

    public class EnterpriseUiBootstrap : IExtensionApplication
    {
        private static PaletteSet _bomPalette;

        public void Initialize()
        {
            if (ComponentManager.Ribbon != null) BuildCorporateRibbon();
            else ComponentManager.ItemInitialized += OnComponentItemInitialized;
        }

        private void OnComponentItemInitialized(object sender, RibbonItemEventArgs e)
        {
            if (ComponentManager.Ribbon != null)
            {
                ComponentManager.ItemInitialized -= OnComponentItemInitialized;
                BuildCorporateRibbon();
            }
        }

        private void BuildCorporateRibbon()
        {
            RibbonControl ribbon = ComponentManager.Ribbon;
            if (ribbon.FindTab("CORP_BIM_TAB") != null) return;

            RibbonTab tab = new RibbonTab { Title = "Enterprise BIM", Id = "CORP_BIM_TAB" };
            ribbon.Tabs.Add(tab);

            RibbonPanelSource panelSrc = new RibbonPanelSource { Title = "QA & ERP Sync" };
            RibbonPanel panel = new RibbonPanel { Source = panelSrc };
            tab.Panels.Add(panel);

            RibbonButton btnSync = new RibbonButton
            {
                Text = "Sync SAP BOM",
                ShowText = true,
                Size = RibbonItemSize.Large,
                Orientation = System.Windows.Controls.Orientation.Vertical,
                CommandParameter = "._GSTAR_SAP_SYNC ",
                CommandHandler = new RibbonCommandRouter()
            };
            panelSrc.Items.Add(btnSync);
        }

        public void Terminate() { }
    }
}

3. PaletteSet GUID Persistence & Dark/Light Theme Synchronization (`COLORTHEME`)

When instantiating a dockable `PaletteSet`, always pass a deterministic **UUID (`new Guid("...")`)** to the constructor: new PaletteSet("ERP BOM Inspector", "GSTAR_BOM_PALETTE", new Guid("8F4B2E91-C0D7-43F5-E6B1-2904A8C3D2E1")). GstarCAD 2027 uses this GUID to persist the user's preferred docking edge (Left, Right, Floating, Auto-Rollup, and pixel width) inside the Windows Registry/Profile XML across sessions.

  • Synchronizing WPF Colours with `COLORTHEME`: Subscribe to `Application.SystemVariableChanged`. When `e.Name == "COLORTHEME"`, query `Convert.ToInt16(Application.GetSystemVariable("COLORTHEME"))` (`0 = Dark Theme #1E242B`, `1 = Light Theme #F3F4F6`) and swap your WPF `MergedDictionaries` resource theme immediately.
  • Modeless Document Context: Clicking a button inside a docked WPF `PaletteSet` executes outside the CAD command fiber. Either route the action through `doc.SendStringToExecute()` or wrap database transactions inside `using (DocumentLock loc = doc.LockDocument()) { ... }`.

4. High-DPI Vector Icon Rendering (4K Monitors) & Contextual Ribbon Tabs

On modern 4K engineering workstations running at 150% or 200% Windows DPI scaling, bitmap `.png` icons can appear soft if only 16×16 and 32×32 pixel assets are supplied. In GstarCAD 2027 `.NET`, assign a WPF `DrawingImage` (vector XAML geometry) or a 64×64 high-DPI `BitmapImage` to `RibbonButton.LargeImage` and `RibbonButton.Image` so icons scale with razor-sharp vector clarity across any monitor DPI.

You can also configure **Contextual Ribbon Tabs** that appear automatically when a user selects a specific block or layer by subscribing to `Application.DocumentManager.MdiActiveDocument.ImpliedSelectionChanged` and toggling `tab.IsVisible` and `tab.IsContextualTab = true`.

Validate This Workflow in GstarCAD 2027

Engineered by Suzhou Gstarsoft Co., Ltd. (SSE: 688657), GstarCAD 2027 delivers 100% native AC1032 DWG binary fidelity, perpetual licensing sovereignty, and zero-day AutoCAD command parity.

Download Free 30-Day Evaluation → Explore AutoCAD Migration Hub Take Skill Certification Quiz