Getting Legend To DIsplay Symbols for Graphics Layer

4708
5
09-19-2011 02:31 PM
NicoleSchmidt
New Contributor III
I cannot seem to get the legend to display symbols for my graphics layers.  Should the legend be able to do this if a Renderer is used?

I took the exact sample from here:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendSimple
and modified the code behind to add the layer, and referenced a custom symbol.  The graphic does show up in the map, and the symbol is properly rendered, but the legend does not display.  Is this the expected behavior?

Thanks for any ideas or insight you may have

  
public MainPage()
{
 InitializeComponent();

 GraphicsLayer gl = new GraphicsLayer();
 Graphic gr = new Graphic();
 gr.Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(-110,34);
 gr.Attributes.Add("Sequence", 1);
 gl.Graphics.Add(gr);
 gl.ID = "my test layer";
 SimpleRenderer sr = new SimpleRenderer();
 sr.Symbol = new RoutingStopSymbols(new SolidColorBrush(Colors.Red));
 MyMap.Layers.Add(gl);
   
}


public class RoutingStopSymbols : ESRI.ArcGIS.Client.Symbols.MarkerSymbol, ESRI.ArcGIS.Client.IRenderer 
{
 public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Brush), typeof(RoutingStopSymbols), null);

 public RoutingStopSymbols(Brush brush)
 {
  this.Color = brush;
  ResourceDictionary resDict = new ResourceDictionary();
  resDict.Source = new Uri("/SilverlightApplication7;component/Routing.xaml", UriKind.Relative);
  ControlTemplate = resDict["RoutePoint"] as ControlTemplate;
 }
 public Brush Color
 {
  get
  {
   return (Brush)GetValue(ColorProperty);
  }
  set
  {
   SetValue(ColorProperty, value);
  }
 }

 public ESRI.ArcGIS.Client.Symbols.Symbol GetSymbol(ESRI.ArcGIS.Client.Graphic graphic)
 {
  return this;
 }
}


<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>

   <ControlTemplate x:Key="RoutePoint" >
    <Grid RenderTransformOrigin="0.5,0.5" Width="25" Height="25" >
     <Grid.RenderTransform>
      <TransformGroup>
       <ScaleTransform ScaleX="1" ScaleY="1" />
      </TransformGroup>
     </Grid.RenderTransform>
     <Ellipse x:Name="backgroundCircle" Stroke="black" 
         Width="25" Height="25" 
         Fill="{Binding Symbol.Color}" 
         HorizontalAlignment="Center" VerticalAlignment="Center" />

     <TextBlock Text="{Binding Attributes[Sequence]}" 
         Foreground="White"
         HorizontalAlignment="Center" VerticalAlignment="Center" 
         FontStretch="Expanded" FontSize="12" FontWeight="ExtraBlack"   />
    </Grid>
   </ControlTemplate>

</ResourceDictionary>
0 Kudos
5 Replies
JohnnyPenet
New Contributor
Nicole,

There is nothing wrong with the code, but you have to refresh the legend after doing modifications to the map control. The legend is not notified about changes to the map.

Johnny
0 Kudos
NicoleSchmidt
New Contributor III
Johnny,
Adding a refresh does not seem to change anything.  I still do not get any symbology on my legend for my Graphics Layer.

Thanks,
Nicole
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I cannot seem to get the legend to display symbols for my graphics layers. Should the legend be able to do this if a Renderer is used?

Yes, it should.




public MainPage()
{
InitializeComponent();

GraphicsLayer gl = new GraphicsLayer();
Graphic gr = new Graphic();
gr.Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(-110,34);
gr.Attributes.Add("Sequence", 1);
gl.Graphics.Add(gr);
gl.ID = "my test layer";
SimpleRenderer sr = new SimpleRenderer();
sr.Symbol = new RoutingStopSymbols(new SolidColorBrush(Colors.Red));
MyMap.Layers.Add(gl);

}


It seems you never affect the renderer to the graphicslayer!
Is your symbol correctly displayed in the map?
0 Kudos
NicoleSchmidt
New Contributor III
Dominique,
Thanks that was my problem in my mockup.  Once I set the Renderer it works.  Ultimately I am trying to figure out why some other code I have is not working and created these mockups to help figure it out.

I think I now realize why my original code was not working.  I had a custom renderer, which implemented IRenderer, and I think I now realize it also needs to implement ILegendSupport as well.  Thank you for your help.

Nicole
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Right. Custom renderers need to implement ILegendSupport if you want to see legenditems in the legend control.
0 Kudos