Select to view content in your preferred language

Changing Ribbon Icon at Runtime

397
1
07-26-2024 03:14 PM
khamillejaynes20
New Contributor

Hello, is there any way to change an icon on the ribbon at runtime? For example, if a state or condition changes, can i also change the picture that shows up on the ribbon? 

Tags (3)
0 Kudos
1 Reply
GKmieliauskas
Esri Regular Contributor

Hi,

For Button or MapTool you can override OnUpdate method:

        protected override void OnUpdate()
        {
                if (myCondition)
                {
                    Caption = "Title1";
                    SmallImage = BuildImage("small_image1.png");
                    LargeImage = BuildImage("large_image1.png");
                }
                else
                {
                    Caption = "Title2";
                    SmallImage = BuildImage("small_image2.png");
                    LargeImage = BuildImage("large_image2.png");
                }
        }

Or set from code:

private void ChangeCaptionImage()
{
  IPlugInWrapper wrapper = FrameworkApplication.GetPlugInWrapper("MyAddin_MyCustomButton");
  if (wrapper != null)
  {
    wrapper.Caption = "new caption";

    // ensure that T-Rex16 and T-Rex32 are included in your add-in under the images folder and have 
    // BuildAction = Resource and Copy to OutputDirectory = Do not copy
    wrapper.SmallImage = BuildImage("T-Rex16.png");
    wrapper.LargeImage = BuildImage("T-Rex32.png");
  }
}

private ImageSource BuildImage(string imageName)
{
  return new BitmapImage(PackUriForResource(imageName));
}

private Uri PackUriForResource(string resourceName)
{
  string asm = System.IO.Path.GetFileNameWithoutExtension(
      System.Reflection.Assembly.GetExecutingAssembly().Location);
  return new Uri(string.Format("pack://application:,,,/{0};component/Images/{1}", asm, resourceName), UriKind.Absolute);
}
0 Kudos