Select to view content in your preferred language

Adding Graphic.Attributes in XAML

3893
8
11-11-2010 07:22 AM
DanielWalton
Frequent Contributor
I'm trying to add graphics to my map in XAML using this syntax:

  [HTML]<esri:GraphicsLayer ID="Shapes">
       <esri:Graphic>
          <esri:Graphic.Attributes>
             <sys:String x:Key="Name">Graphic 0</sys:String>
          </esri:Graphic.Attributes>
          <esri:Graphic.Geometry>....</esri:Graphic.Geometry>
       </esri:Graphic>
  </esri:GraphicsLayer>[/HTML]I get a runtime exception (I assume because Attributes is read-only). Is there a way to add attributes with XAML?
0 Kudos
8 Replies
JenniferNery
Esri Regular Contributor
If you are using SL4, the only way I could think of about doing this is as follows:

   <TextBox  Width="50"  Text="{Binding [STATE_NAME], Mode=TwoWay}" DataContext="{Binding ElementName=MyMap, Path=Layers[MyGraphics].Graphics[0].Attributes}"/>


Where MyMap is the name of my map, MyGraphics is the ID of my Layer, and Graphics[0] is expected to exist.

This is the sample, I placed breakpoint on MouseLeftButtonDown to inspect the Attributes. You can also do a MessageBox.Show() on e.Graphic.Attributes["STATE"] to see that it matches your TextBox. It's not a pretty solution because each TextBox would have to represent a field for one of the graphics.
 <Grid x:Name="LayoutRoot" Background="White">

  <Grid.Resources>
   <esri:SimpleMarkerSymbol x:Key="RedMarkerSymbol" Color="Red" Size="12" Style="Circle" />
  </Grid.Resources>

  <esri:Map x:Name="MyMap" Background="White">
   <esri:ArcGISTiledMapServiceLayer ID="PhysicalTiledLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/NPS_Physical_World_2D/MapServer"/>

   <esri:GraphicsLayer ID="MyGraphics" MouseLeftButtonDown="GraphicsLayer_MouseLeftButtonDown">
    <esri:GraphicsLayer.Graphics >

     <esri:Graphic Symbol="{StaticResource RedMarkerSymbol}" >
      <esri:MapPoint X="-140.9" Y="63.391" />      
     </esri:Graphic>
    </esri:GraphicsLayer.Graphics>
   </esri:GraphicsLayer>
  </esri:Map>
  <StackPanel VerticalAlignment="Top" HorizontalAlignment="Center" >
   <TextBox  Width="50"  Text="{Binding [STATE_NAME], Mode=TwoWay}" DataContext="{Binding ElementName=MyMap, Path=Layers[MyGraphics].Graphics[0].Attributes}"/>
  </StackPanel>
 
 </Grid>
0 Kudos
DanielWalton
Frequent Contributor
My goal is to 'hard-code' some features with attributes into my app. I am generating the XAML dynamically from a FeatureService dataset, then when (if) I can get it working, I will remove the FeatureService and just have the graphics. If I can't add the attributes with XAML, is there a way to store JSON messages and trick the FeatureService into reading them when it loads?
0 Kudos
JenniferNery
Esri Regular Contributor
You don't want to update the attributes in code-behind? (i.e. graphic.Attributes["STATE"] = "California")
0 Kudos
DanielWalton
Frequent Contributor
No, this is just a one-off operation to capture the geometry and attributes of a certain set of features, then hard-code both into my app.
0 Kudos
JenniferNery
Esri Regular Contributor
I dont know if this would solve your problem but what you can do is query the FeatureService and in the QueryTask_ExecuteCompleted update a GraphicsCollection (In the sample-code below, use the same instance as MyGraphicCollection).
 <Grid.Resources>
  <esri:GraphicCollection x:Key="MyGraphicCollection"/>
<!--more code goes here -->
 </Grid.Resources>
<!--more code goes here -->
 <esri:GraphicsLayer ID="MyGraphicsLayer" 
   Graphics="{Binding Source={StaticResource MyGraphicCollection}, Path=Graphics}" Renderer="{StaticResource MyRenderer}"/>
0 Kudos
DanielWalton
Frequent Contributor
I'm trying to get away from querying a service and load stuff fast.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
One possible option is you to use an XML format to store graphics.
You will find sample code to serialize/deserialize graphics here: http://forums.arcgis.com/threads/8774-save-layer-to-xml-file
0 Kudos
DanielWalton
Frequent Contributor
Perfect. Thanks Dominique!
0 Kudos