How to Create a Custom Toggle Button

<?xml encoding=”UTF-8″>
By
Madhukar Moogala

Here in this blog I have shown how to create a Ribbon Toggle Button using AutoCAD Ribbon Runtime API.


https://blog.autodesk.io/2015/03/23/how-to-use-toggle-button-ribbon-api/

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.

Logic:
  • 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
    <font size="2">    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;
    }</font>
    
  • 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 key of ToggleButton should be same as the Id of ToggleButton in CUI. For example “XyzToggleButton”




<font size="2">        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", "XYZSTATEnToggle", null, RibbonButtonStyle.LargeWithText);
cs.Save();
}</font>
<font size="2"><span><</span><span>adw</span><span>:</span><span>RibbonToggleButton</span>
<span>x</span><span>:</span>Uid<span>=</span><span>"</span><span>RibbonToggleButton-Xyz</span><span>"</span>
<span>x</span><span>:</span>Key<span>=</span><span>"</span><span>XyzToggleButton</span><span>"</span>
Name<span>=</span><span>"</span><span>XYZ Toggle</span><span>"</span>
Tag<span>=</span><span>"</span><span>XYZSTATE</span><span>"</span><span>></span>
<span><</span><span>adw</span><span>:</span><span>RibbonToggleButton.IsCheckedBinding</span><span>></span>
<span><</span><span>Binding</span> Source<span>=</span><span>"</span><span>{x:Static acmgd:Application.UIBindings}</span><span>"</span> Path<span>=</span><span>"</span><span>SystemVariables[XYZSTATE].Value</span><span>"</span> Converter<span>=</span><span>"</span><span>{StaticResource XyzToggleButtonConverter}</span><span>"</span><span>/></span>
<span></</span><span>adw</span><span>:</span><span>RibbonToggleButton.IsCheckedBinding</span><span>></span>
<span><</span><span>adw</span><span>:</span><span>RibbonToggleButton.Image</span><span>></span>
<span><</span><span>BitmapImage</span> <span>x</span><span>:</span>Uid<span>=</span><span>"</span><span>BitmapImage_1</span><span>"</span> UriSource<span>=</span><span>"</span><span>Resources/Toggle.bmp</span><span>"</span> <span>/></span>
<span></</span><span>adw</span><span>:</span><span>RibbonToggleButton.Image</span><span>></span>
<span><</span><span>adw</span><span>:</span><span>RibbonToggleButton.LargeImage</span><span>></span>
<span><</span><span>BitmapImage</span> <span>x</span><span>:</span>Uid<span>=</span><span>"</span><span>BitmapImage_2</span><span>"</span> UriSource<span>=</span><span>"</span><span>Resources/Toggle.bmp</span><span>"</span><span>/></span>
<span></</span><span>adw</span><span>:</span><span>RibbonToggleButton.LargeImage</span><span>></span>
<span></</span><span>adw</span><span>:</span><span>RibbonToggleButton</span><span>></span></font>

Complete sample with source code is available at Github : SimpleToggleButton


Comments

9 responses to “How to Create a Custom Toggle Button”

  1. The details are very unique, so I must adhere to you.

  2. I always enjoy reading the articles that are posted here. You are doing a great work

  3. Thanks for your information, it was really very helpfull.

  4. I have read this article. I think You put a lot of effort into creating this article. I appreciate your work.

  5. Tyrone Pitts Avatar
    Tyrone Pitts

    It has limited compatibility with which version of the machine to perform button switching geometry dash lite.

  6. bensonclark Avatar
    bensonclark

    Thanks for sharing this best stuff with us! Keep sharing! I am new in blog writing. All types of blogs and posts are not helpful for the readers. Here the author is giving good thoughts and suggestions to each and every reader through this article. pay to write my assignment literature-essay Quality of the content is the main element of the blog and this is the way of writing and presenting.

  7. What’s going on I’m new to this, I just discovered this by accident. I found It very helpful and it helped me a lot. I hope to contribute and help other users like it helped me. You did a great job.

  8. That is very thorough and straightforward for us to understand.

  9. This is, in my opinion, one of the best posts that you have made. Your work is quite outstanding in both quality and quantity. I am grateful to you for it.

Leave a Reply to mapquest driving directionsCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading