Select to view content in your preferred language

How to add a control at runtime

1022
4
10-17-2011 02:50 PM
CharlotteCrawford
Emerging Contributor
Hi there,

How would I go about adding a control, e.g stackpanel with a textblock in it to the map at runtime in a specific place? The effect I am wanting to achieve is similar to the silverlight control at this url -
http://maps.marlborough.govt.nz/FloodWatch/.  As you can see this control has blue labels at various places on the map. I'd like to do something similar, but don't quite know how to get started.


Thanks
CC
0 Kudos
4 Replies
HugoCardenas
Emerging Contributor
First, you should add your parent control (StackPanel) at design time using XAML.  Place it, anywhere on the map area and hide it, and add the textblock control as well.
<StackPanel x:Name="MyPanel" Visibility="Collapsed" ...>
   <TextBlock x:Name="MyTextblock" Text=""  />
</StackPanel>


Next, add whatever text you want to add to your textblock at run-time from code-behind; once you are done adding the text, you can set Visibility=Visibility.Visible to the StackPanel.
MyTextblock.Text = "The text to display";
MyPanel.Visibility = Visibility.Visible;


This would be the simplest way to do it.

I hope this helps!
Hugo.
0 Kudos
CharlotteCrawford
Emerging Contributor
Thanks for the help. It does get me a bit further.  I need to be able to add multiple instances of the control at runtime.   How can I do that?

I also want to position the controls near certain items in my graphics layer.  How do I relate the position of the control to something in a graphics layer?
0 Kudos
JenniferNery
Esri Regular Contributor
You can probably use GraphicsLayer with custom symbol that gets a value from the graphic.Attributes.

For example you can update this sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSimple to include the following code:
<esri:SimpleRenderer x:Key="MyRenderer">
    <esri:MarkerSymbol OffsetX="10" OffsetY="10">
     <esri:MarkerSymbol.ControlTemplate>
      <ControlTemplate>
       <StackPanel>        
        <TextBlock Text="{Binding Attributes[CITY_NAME]}" />
       </StackPanel>
      </ControlTemplate>
     </esri:MarkerSymbol.ControlTemplate>
    </esri:MarkerSymbol>
   </esri:SimpleRenderer>
<!--more code goes here-->
<esri:GraphicsLayer ID="MyGraphicsLayer" Renderer="{StaticResource MyRenderer}"/>


private void FeatureLayer_UpdateCompleted(object sender, EventArgs e)
{
 var source = sender as FeatureLayer;
 var target = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
 foreach (var g in source.Graphics)
 {
  var graphic = new Graphic() { Geometry = g.Geometry };
  foreach (var attribute in g.Attributes)
   graphic.Attributes[attribute.Key] = attribute.Value;
  target.Graphics.Add(graphic);
 }
}
0 Kudos
CharlotteCrawford
Emerging Contributor
Thanks Jennifer,  that looks like a good way to do it.  I always learn such good techniques from your posts, so keep up the good work!

CC
0 Kudos