ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / ScaleBarStyleItem Class
Members Example

In This Topic
    ScaleBarStyleItem Class
    In This Topic
    Represents a scale bar saved in a style.
    Object Model
    ScaleBarStyleItem ClassCIMScaleBar Class
    Syntax
    public class ScaleBarStyleItem : StyleItem, System.ComponentModel.INotifyPropertyChanged  
    Public Class ScaleBarStyleItem 
       Inherits StyleItem
       Implements System.ComponentModel.INotifyPropertyChanged 
    Example
    Create_ScaleBar
    //Create a scale bar for a specific map frame and assign a scale bar style item.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference a North Arrow in a style
      StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name == "ArcGIS 2D");
      ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars("Double Alternating Scale Bar 1")[0];
    
      //Build geometry
      Coordinate2D center = new Coordinate2D(7, 8);
    
      //Reference MF, create north arrow and add to layout 
      MapFrame mf = layout.FindElement("New Map Frame") as MapFrame;
      if (mf == null)
      {
        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Map frame not found", "WARNING");
        return;
      }
    
      //At 2.x -
      //ScaleBar sbElm = LayoutElementFactory.Instance.CreateScaleBar(layout, center, mf, sbStyleItm);
      //sbElm.SetName("New Scale Bar");
      //sbElm.SetWidth(2);
      //sbElm.SetX(6);
      //sbElm.SetY(7.5); 
    
      var sbInfo = new ScaleBarInfo()
      {
        MapFrameName = mf.Name,
        ScaleBarStyleItem = sbStyleItm
      };
      var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
                                          layout, center.ToMapPoint(), sbInfo, "New Scale Bar") as ScaleBar;
      sbElm.SetWidth(2);
      sbElm.SetX(6);
      sbElm.SetY(7.5);
    });
    Create Scale Bar From StyleItem
    //Create a scale bar using a style.
    
    //Search for a style project item by name
    StyleProjectItem arcgis_2dStyle = Project.Current.GetItems<StyleProjectItem>()
                                .First(si => si.Name == "ArcGIS 2D");
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference the specific scale bar by name 
      ScaleBarStyleItem scaleBarItem = arcgis_2dStyle.SearchScaleBars(
                         "Double Alternating Scale Bar").FirstOrDefault();
    
      //Reference the map frame and define the location
      MapFrame myMapFrame = layout.FindElement("Map Frame") as MapFrame;
      Coordinate2D coord2D = new Coordinate2D(10.0, 7.0);
    
      //Construct the scale bar
      //At 2.x - LayoutElementFactory.Instance.CreateScaleBar(
      //             layout, coord2D, myMapFrame, scaleBarItem);
      var sbarInfo = new ScaleBarInfo()
      {
        MapFrameName = myMapFrame.Name,
        ScaleBarStyleItem = scaleBarItem
      };
      ElementFactory.Instance.CreateMapSurroundElement(
              layout, coord2D.ToMapPoint(), sbarInfo);
    });
    Create Scale Bar
    //Must be on QueuedTask.Run(() => { ...
    
    //Build geometry
    Coordinate2D ll = new Coordinate2D(5.0, 6);
    Coordinate2D ur = new Coordinate2D(6.0, 7);
    Envelope sbEnv = EnvelopeBuilderEx.CreateEnvelope(ll, ur);
    
    //Reference a Scale Bar in a style
    StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>()
                  .FirstOrDefault(item => item.Name == "ArcGIS 2D");
    ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
                 "Alternating Scale Bar 1")[0];
    //ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
    //                                   "Double Alternating Scale Bar 1")[0];
    //ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
    //                                    "Hollow Scale Bar 1")[0];
    
    //Create Scale Bar
    ScaleBarInfo sbInfo = new ScaleBarInfo()
    {
      MapFrameName = mapFrame.Name
    };
    
    var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
                                       layout, sbEnv, sbInfo) as ScaleBar;
    Create Scale Line
    //Must be on QueuedTask.Run(() => { ...
    
    //Build geometry
    Coordinate2D ll = new Coordinate2D(5.0, 8);
    Coordinate2D ur = new Coordinate2D(6.0, 9);
    Envelope sbEnv = EnvelopeBuilderEx.CreateEnvelope(ll, ur);
    
    //Reference a Scale Bar in a style
    StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>()
                                 .FirstOrDefault(item => item.Name == "ArcGIS 2D");
    ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
                                  "Scale Line 1")[0];
    //ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
    //                                           "Stepped Scale Line")[0];
    //ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
    //                                            "Scale Line 2")[0];
    
    //Create Scale Bar
    ScaleBarInfo sbInfo = new ScaleBarInfo()
    {
      MapFrameName = mapFrame.Name,
      ScaleBarStyleItem = sbStyleItm
    };
    
    var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
                      layout, sbEnv, sbInfo, "ScaleBar Line") as ScaleBar;
    How to search for scale bars in a style
    public Task<IList<ScaleBarStyleItem>> GetScaleBarsFromStyleAsync(StyleProjectItem style, string searchString)
    {
      if (style == null)
        throw new System.ArgumentNullException();
    
      //Search for scale bars
      return QueuedTask.Run(() => style.SearchScaleBars(searchString));
    }
    Inheritance Hierarchy

    System.Object
       ArcGIS.Desktop.Framework.Contracts.PropertyChangedBase
          ArcGIS.Desktop.Mapping.StyleItem
             ArcGIS.Desktop.Mapping.ScaleBarStyleItem

    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also