Select to view content in your preferred language

GraphicsLayer InfoWindow with Attributes Added Dynamically

1949
6
10-03-2011 10:32 AM
ThomasSchultz
Deactivated User
I have some a control that allows a user to add a shapefile to the map as a graphics layer.  I added a MouseLeftButtonDown event that opens an InfoWindow.  I would like InfoWindow to have all of the attributes from the graphicLayer, but I don???t how to bind the attributes to InfoWindow text without knowing their names first.

I guess what I would like to have happen is for a textblock to be added to my InfoWindow template for each attribute field in my graphic, and for the text of the textblock to be the attribute value.  Anyone have an idea how to get this to work?  Here are the pertinent portions of my code:

void GraphicClick(object sender, GraphicMouseButtonEventArgs e)
        {
            Graphic gra = e.Graphic;
            MapPoint myMapPoint = new MapPoint();
            myMapPoint = gra.Geometry as MapPoint;
            InfoWindow myInfoWindow = new InfoWindow();
            myInfoWindow.Content = e.Graphic.Attributes;
            myInfoWindow.Anchor = myMapPoint;
            myInfoWindow.IsOpen = true;
            myInfoWindow.Map = MapApplication.Current.Map;
            myInfoWindow.ContentTemplate = LayoutRoot.Resources["InfoWindowTemplate"] as System.Windows.DataTemplate;
            LayoutRoot.Children.Add(myInfoWindow);
            myInfoWindow.MouseLeftButtonDown += new MouseButtonEventHandler(MyInfoWindow_MouseLeftButtonDown);
        }

<Grid.Resources>
            <DataTemplate x:Key="InfoWindowTemplate" x:Name="InfoWindowTemplate">
                <StackPanel x:Name="InfoStack" Orientation="Vertical" Margin="10,10,10,10" Background="Transparent">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Background="Transparent">
                        <TextBlock Text="Field Name: " FontWeight="Bold"/>
                        <TextBlock x:Name="textValue" >
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </Grid.Resources> 
0 Kudos
6 Replies
JenniferNery
Esri Regular Contributor
You can use the following InfoWindow.ContentTemplate:
<DataTemplate x:Key="MyDynamicMapInfoWindowTemplate">
 <Grid>
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="Auto"/>
   <ColumnDefinition Width="*"/>
  </Grid.ColumnDefinitions>
  <ItemsControl ItemsSource="{Binding Keys}" Grid.Column="0" />
  <ItemsControl ItemsSource="{Binding Values}" Grid.Column="1" />
 </Grid>
</DataTemplate>


The above code will use Keys and Values from your Attributes.
0 Kudos
ThomasSchultz
Deactivated User
Jennifer,

Thanks so much for the reply.  I'm having difficulty getting this to work - nothing opens up when I click on the graphic.  Do I need to put the DataTemplate somewhere specific in my control?  Right now, I have it in a Grid.Resources section.  And I am calling it with:  myInfoWindow.ContentTemplate = LayoutRoot.Resources["MyDynamicMapInfoWindowTemplate"] as System.Windows.DataTemplate;.
0 Kudos
JenniferNery
Esri Regular Contributor
The following code is from SDK sample. Ensure you have the highlighted lines.

  foreach (Graphic g in selected)
            {

                MyInfoWindow.Anchor = e.MapPoint;
                MyInfoWindow.IsOpen = true;
                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                MyInfoWindow.Content = g.Attributes;
                return;
            }

            InfoWindow window = new InfoWindow()
            {
                Anchor = e.MapPoint,
                Map = MyMap,
                IsOpen = true,
                ContentTemplate = LayoutRoot.Resources["MyDynamicMapInfoWindowTemplate"] as System.Windows.DataTemplate,
                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                Content = e.MapPoint 
            };
0 Kudos
ThomasSchultz
Deactivated User
Hi Jennifer,

I double-checked that I had the highlighted lines, but still no attributes in my infoWindow.  Here is my control:

[HTML]<DataTemplate x:Key="MyDynamicMapInfoWindowTemplate" x:Name="MyDynamicMapInfoWindowTemplate">
                <StackPanel x:Name="InfoStack" Orientation="Vertical" Margin="10,10,10,10" Background="Transparent">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <ItemsControl ItemsSource="{Binding Keys}" Grid.Column="0" />
                        <ItemsControl ItemsSource="{Binding Values}" Grid.Column="1" />
                    </Grid>
                </StackPanel>               
            </DataTemplate>[/HTML]

And my code behind:

[HTML]Graphic gra = e.Graphic;         
            MapPoint myMapPoint = new MapPoint();
            myMapPoint = gra.Geometry as MapPoint;
            InfoWindow myInfoWindow = new InfoWindow();
            myInfoWindow.Content = e.Graphic.Attributes;
            myInfoWindow.Anchor = myMapPoint;
            myInfoWindow.IsOpen = true;
            myInfoWindow.Map = MapApplication.Current.Map;
            myInfoWindow.ContentTemplate = LayoutRoot.Resources["MyDynamicMapInfoWindowTemplate"] as System.Windows.DataTemplate;
            LayoutRoot.Children.Add(myInfoWindow);           
            myInfoWindow.MouseLeftButtonDown += new MouseButtonEventHandler(MyInfoWindow_MouseLeftButtonDown);[/HTML]
0 Kudos
JenniferNery
Esri Regular Contributor
Graphic.Attributes is not empty?

If you have this in your XAML, the highlighted is what needs to be replaced not the "LocationInfoWindowTemplate" in code-behind:
<esri:InfoWindow x:Name="MyInfoWindow"
                         Padding="2"
                         CornerRadius="20" 
                         Background="LightSalmon"
                         Map="{Binding ElementName=MyMap}" 
                         ContentTemplate="{StaticResource MyDynamicMapInfoWindowTemplate}"
                         MouseLeftButtonUp="MyInfoWindow_MouseLeftButtonUp" />


I tested this with http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#InfoWindowSimple
0 Kudos
ThomasSchultz
Deactivated User
Jennifer,

I am so sorry.  The main reason I can't get this working is because of an issue causing my graphic.Attributes to be empty.  I checked that they were there when read from the shapefile and added to the first graphics layer.  But I am then using the ProjectAsync method to project that graphicslayer, after which the new graphics have lost their attributes.

Thanks for helping with the initial issue.  I am probably going to post a new topic, since I have no idea why projecting the graphics is causing them to lose their graphics - if you have any ideas why this might be, I'd appreciate it.

Thanks,

Thomas
0 Kudos