Here in this blog I have shown how to create a Ribbon Toggle Button using AutoCAD Ribbon Runtime API.
https://adndevblog.typepad.com/autocad/2015/03/how-to-use-toggle-button-ribbon-api.html
This code sample shows how can we register a custom ribbon control on AutoCAD Ribbon bar, to illustrate I created a simple toggle button, but however this logic can be extended to any other fancy controls.
-
Create a ResourceDictionary to hold resources and controls in XAML
-
Implement the binding logic to manage the state of ToggleButton.
- Create a SystemVariable to hold the state of ToggleButton
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return IsChecked(value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { bool isOn = (bool)value; if (isOn && !string.IsNullOrEmpty(OnMacro)) RunMacro(OnMacro); else if (!isOn && !string.IsNullOrEmpty(OffMacro)) RunMacro(OffMacro); return DataBindings.DoNothing; }
-
Load this XAML in to AutoCAD Runtime, so this custom dictionary gets merged in to the AutoCAD main resource dictionary and you will see a unfied UX.
-
If you are preparing CUIX using CUI editor, make sure, the
keyof ToggleButton should be same as theIdof ToggleButton in CUI. For example "XyzToggleButton"
string menuName = (string)Application.GetSystemVariable("MENUNAME");
CustomizationSection cs = new CustomizationSection(menuName + ".cuix");
var ribbonRoot = cs.MenuGroup.RibbonRoot;
var homeTab = ribbonRoot.FindTab("ID_TabHome");
var elementId = "ID_TogglePanel";
var ribbonPanelSourceReference = homeTab.Find(elementId);
if(ribbonPanelSourceReference is null)
{
var panel = homeTab.AddNewPanel(elementId, "TogglePanel");
var row = panel.AddNewRibbonRow();
row.AddNewToggleButton("XyzToggleButton", "XYZSTATE\nToggle", null, RibbonButtonStyle.LargeWithText);
cs.Save();
} <adw:RibbonToggleButton
x:Uid="RibbonToggleButton-Xyz"
x:Key="XyzToggleButton"
Name="XYZ Toggle"
Tag="XYZSTATE">
<adw:RibbonToggleButton.IsCheckedBinding>
<Binding Source="{x:Static acmgd:Application.UIBindings}" Path="SystemVariables[XYZSTATE].Value" Converter="{StaticResource XyzToggleButtonConverter}"/>
</adw:RibbonToggleButton.IsCheckedBinding>
<adw:RibbonToggleButton.Image>
<BitmapImage x:Uid="BitmapImage_1" UriSource="Resources/Toggle.bmp" />
</adw:RibbonToggleButton.Image>
<adw:RibbonToggleButton.LargeImage>
<BitmapImage x:Uid="BitmapImage_2" UriSource="Resources/Toggle.bmp"/>
</adw:RibbonToggleButton.LargeImage>
</adw:RibbonToggleButton>TE">How to: Convert Bound Data - WPF .NET Framework | Microsoft Learn
git clone https://github.com/MadhukarMoogala/SimpleToggleButton.git
cd SimpleToggleButton
msbuild SimpleToggleButton.csproj -property:Configuration=DebugExpected Build Log: here
This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.
Madhukar Moogala, Forge Partner Development @galakar
