By Adam Nagy
I'm creating a RibbonSplitButton, but the RibbonButtons I add to it do not show their icons. What could be the problem?
Solution
Probably you are just missing the line where you set RibbonButton.ShowImage = true;
In case something else is missing attached is a project which creates the following split button on the ribbon using png images included in the assembly as resources:
Here is the source code of the project:
using System;
using System.Windows.Media.Imaging;
using System.Reflection;
using Autodesk.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace MySplitButton
{
public class Commands
{
BitmapImage getBitmap(string fileName)
{
BitmapImage bmp = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bmp.BeginInit();
bmp.UriSource = new Uri(string.Format(
"pack://application:,,,/{0};component/{1}",
Assembly.GetExecutingAssembly().GetName().Name,
fileName));
bmp.EndInit();
return bmp;
}
[CommandMethod("RibbonSplitButton")]
public void RibbonSplitButton()
{
Autodesk.Windows.RibbonControl ribbonControl =
Autodesk.Windows.ComponentManager.Ribbon;
/////////////////////////////////////
// create Ribbon tab
RibbonTab Tab = new RibbonTab();
Tab.Title = "Test Ribbon";
Tab.Id = "TESTRIBBON_TAB_ID";
ribbonControl.Tabs.Add(Tab);
/////////////////////////////////////
// create Ribbon panel
Autodesk.Windows.RibbonPanelSource srcPanel =
new RibbonPanelSource();
srcPanel.Title = "Panel1";
RibbonPanel Panel = new RibbonPanel();
Panel.Source = srcPanel;
Tab.Panels.Add(Panel);
//////////////////////////////////////////////////
// create the buttons listed in the split button
Autodesk.Windows.RibbonButton button1 = new RibbonButton();
button1.Text = "Button1";
button1.ShowText = true;
button1.ShowImage = true;
button1.LargeImage = getBitmap("a_large.png");
button1.Image = getBitmap("a_small.png");
button1.CommandHandler = new MyCmdHandler();
Autodesk.Windows.RibbonButton button2 = new RibbonButton();
button2.Text = "Button2";
button2.ShowText = true;
button2.ShowImage = true;
button2.LargeImage = getBitmap("b_large.png");
button2.Image = getBitmap("b_small.png");
button2.CommandHandler = new MyCmdHandler();
////////////////////////
// create split button
RibbonSplitButton ribSplitButton = new RibbonSplitButton();
// Required to avoid upsetting AutoCAD when using cmd locator
ribSplitButton.Text = "RibbonSplitButton";
ribSplitButton.ShowText = true;
ribSplitButton.Items.Add(button1);
ribSplitButton.Items.Add(button2);
srcPanel.Items.Add(ribSplitButton);
Tab.IsActive = true;
}
public class MyCmdHandler : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Document doc = acApp.DocumentManager.MdiActiveDocument;
if (parameter is RibbonButton)
{
RibbonButton button = parameter as RibbonButton;
doc.Editor.WriteMessage(
"\nRibbonButton Executed: " + button.Text + "\n");
}
}
}
}
}
If you want your button to look like this instead, then add the below lines to the above code.
ribSplitButton.IsSplit = true;
ribSplitButton.Size = RibbonItemSize.Large;
ribSplitButton.IsSynchronizedWithCurrentItem = true;
Here is the project: Download Mysplitbutton