Select to view content in your preferred language

question regarding programmatically created graphicslayer & maptips

4569
16
10-19-2010 08:56 AM
MikeDavis
Emerging Contributor
I'm using the beta 2.1 API and taking an RSS feed that has lat/long coordinates and adding it to a GraphicsLayer that I've programmatically created - I've added some of the attributes to each of the graphics as such and added them to a graphicslayer called pGraphicsLayer:

          
graphic.Attributes.Add("Title", item.Title.Text)
graphic.Attributes.Add("Summary", item.Summary.Text)
graphic.Attributes.Add("PublishDate", item.PublishDate)
                    If item.Links.Count > 0 Then
                        graphic.Attributes.Add("Link", item.Links(0).Uri)
                    End If
graphic.Attributes.Add("FeedItem", item)
graphic.Attributes.Add("Id", item.Id)



I've put a UserControl on my XAML page as such:

        <UserControl x:Name="hmTooltip" HorizontalAlignment="Left" Margin="132,141,0,289" Width="140">
<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, ConverterParameter=FeedItem, Mode=OneWay}" 
HorizontalAlignment="Left" VerticalAlignment="Top"  TextWrapping="Wrap" Grid.Row="1" Grid.Column="1" Height="30" Width="190" FontSize="10" Foreground="Black" Margin="2,0,0,0" />
 </UserControl>


But for the life of me I can't figure out how to get the MapTips to work on the pGraphicsLayer object that I programmatically created (the points show up, etc but nothing on hover - it essentially crashes).  I've tried setting the pGraphicsLayer.MapTip = hmTooltip (my user control)

Am I going about this the correct way?  when I hover over i get the following message:

"Message: Unhandled Error in Silverlight 2 Application Value does not fall within the expected range."

Meanwhile I'm using a Silverlight 4 environment so not sure why that's giving me a Silverlight 2 error?
0 Kudos
16 Replies
JenniferNery
Esri Regular Contributor
Have you looked at this sample? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#GraphicsMapTip

You will not need DictionaryConverter if your using Silverlight 4, because this is already supported "{Binding [STATE_NAME]}" where STATE_NAME is just the Attribute Key. If you use the sample above, instead of getting the features from a query to FeatureService, you will create your own graphic, see this other sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#GeoRSS
0 Kudos
dotMorten_esri
Esri Notable Contributor
This error is usually due to invalid XAML.
0 Kudos
stefanschlaefli
Regular Contributor
Hi
did you solve this? I get the same error. I defined the usercontrol (which holds the textbox with the binding) within the resources of the grid where my map control is embedded. I am assigning it from code behind because the featurelayer i use is also created in codebehind. I also tried to do a proper silverlight usercontrol (by adding a silverlight control to the project) but then i would probably have to define a dependency property for the binding. What is the best way to display maptips to a featurelayer from codebehind?
Thanks
Stefan
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The maptip of a graphicslayer is a frameworkelement. This framework element can't be defined as resource.

So, this is NOT working:
 
<Grid.Resources>
  <Border x:key="maptip" >
     .......
  </Border>
</Grid.resources>
........
<esri:FeatureLayer Maptip="{StaticResource maptip}" ........


Nor is this C# code working:
 
myFeatureLayer.MapTip = [FrameworkElement]LayoutRoot.Resources["maptip"];


Instead you can use a DataTemplate:
 
<Grid.Resources>
  <DataTemplate x:key="maptip" >
    <Border >
     .......
    </Border>
  </DataTemplate>
</Grid.resources>
........
<esri:FeatureLayer ...>
 <esri:FeatureLayer.MapTip>
    <ContentControl ContentTemplate="{StaticResource maptip}" Content="{Binding}" />
  </esri:FeatureLayer.MapTip>
</esri:FeatureLayer>


The equivalent C# code is working as well.

Or you can define your own custom maptip control:
 
<esri:FeatureLayer ...>
  <esri:FeatureLayer.MapTip>
    <MyMaptipControl     .............../>
  </esri:FeatureLayer.MapTip>
</esri:FeatureLayer>


or the equivalent in C# : myFeatureLayer.MapTip = new MyMaptipControl() { ......};

Hope this help.
0 Kudos
stefanschlaefli
Regular Contributor
Hi Dominique
Thanks for your help. I tried with a data template:

 
 <DataTemplate x:Name="edmMapTip">
       <Border Background="#FF8EA992" BorderBrush="#FF161537">
            <TextBlock x:Name="tbMapTip" Text="{Binding [VALUE]}" TextWrapping="Wrap" Height = "30" Width="120" FontSize="10" Foreground="Black" Margin="2" />
        </Border>
 </DataTemplate> 


And I call it with this code in c#:
reportLayer.OutFields.Add("OCCUPANCY");
reportLayer.OutFields.Add("VALUE");
FlareClusterer clusterer = new FlareClusterer(); 
reportLayer.Clusterer = clusterer; 
reportLayer.MapTip = grdMapBase.Resources["edmMapTip"] as MapTip;

The mapTips don't display. When I debug, I see that reportLayer.MapTip is always null.
But the resource reference is correct. Has it to do with the fact, that I assign a toolkit mapTip to a featureLayer.MapTip ? How should I do it then?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Hi Stephan,

Your edmMapTip resource is a DataTemplate not a MapTip, so your cast can't work.

You need the C# equivalent of this xaml code:
 
<esri:FeatureLayer.MapTip>
    <ContentControl ContentTemplate="{StaticResource maptip}" Content="{Binding}" />
</esri:FeatureLayer.MapTip>
 


it should be something like:
 
reportLayer.MapTip = new ContentControl()
{
  ContentTemplate = (DataTemplate)grdMapBase.Resources["maptip"]
};
reportLayer.MapTip.SetBinding(ContentControl.ContentProperty, new Binding());
0 Kudos
stefanschlaefli
Regular Contributor
Thanks Dominique. It works with the data template. Since I need multiple data attributes to be displayed I guess I need to work with a usercontrol. Can I define the usercontrol in the xaml of my mainpage or do I need to create and add a "proper" silverlight usercontrol with dependency properties and everything?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I am not sure what you mean by 'I need multiple data attributes to be displayed' but anyway both options are possible.

You can define the maptip in the XAML of the mainpage or you can create your own maptip user control (in another xaml file).  With this last option, if needed, you might add dependency properties to your control.
0 Kudos
stefanschlaefli
Regular Contributor
Dominique

I mean I would like to display multiple data attributes (fields of a featurelayer) in the maptip. I created a simple usercontrol called myMapTip (in another xaml file).
<Border x:Name="bdBase" Background="CadetBlue">
    <StackPanel x:Name="stLabel">
        <TextBlock x:Name="tb" Text="{Binding [VALUE]}"/>     
    </StackPanel>
</Border>

Code behind looks like this:
 
public partial class MyMapTip : UserControl
    {
        public MyMapTip()
        {
            InitializeComponent();
            DataContext = this;
            tb.SetBinding(TextBlock.TextProperty,new System.Windows.Data.Binding{Source = this, Path = new PropertyPath("TipText")}); 
        }

        public static readonly DependencyProperty tbTextProperty = DependencyProperty.Register("Text", typeof(String), typeof(MyMapTip),null);

        public string TipText
        {
            get { return(string) GetValue(tbTextProperty); }
            set { SetValue(tbTextProperty, value); }
        }
        private static void Text_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { }
}


And the control is called like this:
reportLayer.OutFields.Add("VALUE");
FlareClusterer clusterer = new FlareClusterer();
reportLayer.Clusterer = clusterer;
MyMapTip mt = new MyMapTip();     
mt.TipText = "[VALUE]";
reportLayer.MapTip = mt;


It compiles, but the MapTip (in this case the content of the field "VALUE" in the featurelayer) is not displayed. What is wrong with the binding?

Thx again
Stefan
0 Kudos