//Run within QueuedTask context.
//Get the Style project items in the project
var styleProjectItems = Project.Current?.GetItems<StyleProjectItem>();
//Get the ArcGIS 2D Style Project Item
var styleProjectItem =
styleProjectItems.OfType<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS 2D");
if (styleProjectItem == null) return;
//Get the grid style item you need
var gridStyleItem =
styleProjectItem.SearchSymbols(StyleItemType.Grid, "Blue Vertical Label Graticule").FirstOrDefault();
if (gridStyleItem == null) return;
var symbolItemName = gridStyleItem.Name;
var girdGraticuleObject = gridStyleItem.GetObject() as CIMMapGrid;
var mapFrame = layout.GetElements().OfType<MapFrame>().FirstOrDefault();
var cmf = mapFrame.GetDefinition() as CIMMapFrame;
//note, if page units are _not_ inches then grid's gridline
//lengths and offsets would need to be converted to the page units
var mapGrids = new List<CIMMapGrid>();
if (cmf.Grids != null)
mapGrids.AddRange(cmf.Grids);
//var cimMapGrid = SymbolStyleItem.GetObject() as CIMMapGrid;
switch (girdGraticuleObject)
{
case CIMGraticule:
var gridGraticule = girdGraticuleObject as CIMGraticule;
gridGraticule.Name = symbolItemName;
gridGraticule.SetGeographicCoordinateSystem(mapFrame.Map.SpatialReference);
//assign grid to the frame
mapGrids.Add(gridGraticule);
break;
case CIMMeasuredGrid:
var gridMeasure = girdGraticuleObject as CIMMeasuredGrid;
gridMeasure.Name = symbolItemName;
gridMeasure.SetProjectedCoordinateSystem(mapFrame.Map.SpatialReference);
//assign grid to the frame
mapGrids.Add(gridMeasure);
break;
case CIMReferenceGrid:
var gridReference = girdGraticuleObject as CIMReferenceGrid;
gridReference.Name = symbolItemName;
//assign grid to the frame
mapGrids.Add(gridReference);
break;
}
cmf.Grids = mapGrids.ToArray();
mapFrame.SetDefinition(cmf);