HELP: Set MapTip on Graphic not GraphicsLayer

2518
3
04-28-2011 05:47 AM
JustinCornell
New Contributor III
Hello,

I am trying to programatically set a map tip on individual graphics not an entire GraphicLayer. I don't want all graphics in the Graphics layer to have a map tip.  It does not appear that I can use the ESRI.ArcGIS.Client.Toolkit.MapTip because the MapTip wants to be assoicated with an GraphicsLayer or it will throw the error "System.ArgurmentException: Value does not fall within the expected range."  I have created a DataTemplate and tried to set the MapTip on the graphic with the code below.  The MapTip shows but the binding of the attributes does not work.  What am I missing so I can get the Attributes from the graphic to bind to my DataTemplate?  Is there a simpler way to do this without splitting my graphics into multiple graphics layers?

C# code to add the map tip
Graphic lgraphic;
lgraphic.MapTip = new ContentControl() { ContentTemplate = LateralMapTipTemplate };
lgraphic.MapTip.SetBinding(ContentControl.ContentProperty, new System.Windows.Data.Binding());


XAML code for my template
<Grid.Resources>
    <DataTemplate x:Name="LateralMapTipTemplate">
        <Border Background="White">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="OBJID:" Margin="2"/>
                    <TextBlock Text="{Binding [OBJECTID]}" Foreground="White" />
                </StackPanel>
        </Border>
    </DataTemplate>
</Grid.Resources>
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
Graphic.MapTip is a FrameworkElement and cannot be set to a DataTemplate.

According to Silverlight guidelines, Resource can only contain shareable objects, which UIElement is not. http://msdn.microsoft.com/en-us/library/cc903952%28v=vs.95%29.aspx

What you can do is create a UserControl to define the contents of your map tip.
MyMapTip.xaml
    <Grid x:Name="LayoutRoot" Background="White">
  <TextBlock Text="{Binding [Location]}" />
    </Grid>


MainPage.xaml
private void GraphicsLayer_Initialized(object sender, System.EventArgs e)
{
 GraphicsLayer l = sender as GraphicsLayer;
 foreach (var g in l.Graphics)
 {
  g.Attributes["Location"] = g.Geometry;
  g.MapTip = new MyMapTip();
  break;
 }
}
0 Kudos
JustinCornell
New Contributor III
Jennifer - Thanks for the help.  That works.  I thought that since I put a ContentControl as the maptip and set the contentTemplate it would work but no dice.  Thanks again.
0 Kudos
JenniferNery
Esri Regular Contributor
You can also do a pure XAML-code:
 <esri:Graphic Symbol="{StaticResource RedMarkerSymbol}">
   <esri:MapPoint X="-140.9" Y="63.391" />
   <esri:Graphic.MapTip>
     <TextBlock Text="{Binding [Location], FallbackValue=BindingFailed}"/>
   </esri:Graphic.MapTip>
 </esri:Graphic>

or if you have defined a UserControl
 <esri:Graphic Symbol="{StaticResource RedMarkerSymbol}">
  <esri:MapPoint X="-140.9" Y="63.391" />
  <esri:Graphic.MapTip>      
   <local:MyLocationMaptip/>
  </esri:Graphic.MapTip>
 </esri:Graphic>
0 Kudos