Refresh a Legend Properly

2231
1
06-11-2011 12:32 PM
MatSavage1
New Contributor III
Hi,

This is related to an ArcMap add-in I coded up this past week.

I'm trying to rename a layer in ArcMap and have the legend refresh properly. The renaming part is working fine, however I cannot get the legend in the layout to update properly.

I call CurrentContents.Refresh() to refresh the name in the table of contents, which works.

If I search through the map surrounds and call .Refresh() on the legend, then do an ActiveView.PartialRefresh() on it, the legend gets all out of whack. This is why I am trying to simply trigger the same event as when the user manually changes the name of a layer.

The API documentation seems to say that MxDocument.UpdateContents() will trigger the events needed to refresh the legend. However, this does not seem to be the case.
In addition to the above methods I have tried ActiveView.ContentsChanged() to no avail.

Any suggestions are welcome. 

Thanks,

Mat
0 Kudos
1 Reply
MatSavage1
New Contributor III
So I found that I had to remove and re-add the last item in the legend to get it to update properly, without getting messed up in size and font.
        for (int i = 0; i < activeView.FocusMap.MapSurroundCount; i++)
        {
            IMapSurround mapSurround = activeView.FocusMap.MapSurround;
            if (mapSurround is ILegend)
            {
                ILegend legend = mapSurround as ILegend;
                ILegendItem legendItem = legend.get_Item(legend.ItemCount - 1);
                legend.RemoveItem(legend.ItemCount - 1);
                legend.AddItem(legendItem);
            }
        }

I know that this shouldn't be the right way to do this, but it is the only way that works as far as I can tell. It sure seems like there's an event that's not hooked up properly.
0 Kudos