Select to view content in your preferred language

How to create MapTips at runtime

2917
9
01-13-2011 05:59 AM
IgressT
Emerging Contributor
I dynamically create Graphic layers and want to create and assign a new MapTip at runtime each time I create the layer.
The simple way of creating a new MapTip object and assigning its graphics layer property is not working (see the sample code).
Does any one how to do it?

private void GPTask_ExecuteCompleted(object sender, GPExecuteCompleteEventArgs e)
        {
            GraphicsLayer graphicslayer = new GraphicsLayer();
            graphicslayer.ID = "xxx";

            // Adding features to graphics layer
     

            // Rendering    
            graphicslayer.Renderer = cbr;

            // Refresh graphics layer
            graphicslayer.Refresh();

            this.Map.Layers.Add(graphicslayer);

            MapTip mp = new MapTip();
            mp.GraphicsLayer = graphicslayer;
            mp.TitleMember = "grid_code";
            mp.Title = "Pollution Levels";
            mp.Visibility = Visibility.Visible;

        }
0 Kudos
9 Replies
DanielWalton
Frequent Contributor
It looks like you're confusing the two different MapTip classes. The ESRI.ArcGIS.Client.MapTip class would probably work better for what you're trying to do:
        private void GPTask_ExecuteCompleted(object sender, GPExecuteCompleteEventArgs e)
        {
            var graphicslayer = new GraphicsLayer()
            {
                ID = "xxx",
                Renderer = cbr,
                MapTip = new ContentControl()
                { ContentTemplate = (DataTemplate)Resources["gpTaskMapTipTemplate"] }
            };
            graphicslayer.MapTip.SetBinding(ContentControl.ContentProperty, new System.Windows.Data.Binding());
            Map.Layers.Add(graphicslayer);
        }
then on your usercontrol XAML page:
    <UserControl.Resources>
        <DataTemplate x:Key="gpTaskMapTipTemplate">
            <Border Background="White">
                <StackPanel>
                    <TextBlock Text="{Binding [grid_code]}" Foreground="White" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
If you would rather use the ESRI.ArcGIS.Client.Toolkit.MapTip control, you have to declare it separately and add it to your usercontrol's visual tree, whether through XAML or code.
0 Kudos
IgressT
Emerging Contributor
It looks like you're confusing the two different MapTip classes. The ESRI.ArcGIS.Client.MapTip class would probably work better for what you're trying to do:
        private void GPTask_ExecuteCompleted(object sender, GPExecuteCompleteEventArgs e)
        {
            var graphicslayer = new GraphicsLayer()
            {
                ID = "xxx",
                Renderer = cbr,
                MapTip = new ContentControl()
                { ContentTemplate = (DataTemplate)Resources["gpTaskMapTipTemplate"] }
            };
            graphicslayer.MapTip.SetBinding(ContentControl.ContentProperty, new System.Windows.Data.Binding());
            Map.Layers.Add(graphicslayer);
        }
then on your usercontrol XAML page:
    <UserControl.Resources>
        <DataTemplate x:Key="gpTaskMapTipTemplate">
            <Border Background="White">
                <StackPanel>
                    <TextBlock Text="{Binding [grid_code]}" Foreground="White" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
If you would rather use the ESRI.ArcGIS.Client.Toolkit.MapTip control, you have to declare it separately and add it to your usercontrol's visual tree, whether through XAML or code.


Hi that works but the maptip I see when I hover over the graphic is "ESRI.ArcGIS.Client.Graphics.ObservableDictionary"
Any clue?
Thanks
0 Kudos
DanielWalton
Frequent Contributor
So apparently the attribute "grid_code" in your service is of type ObservableCollection. Try replacing the textbox in the maptip with a DataGrid and bind the ItemsSource to [grid_code].
0 Kudos
IgressT
Emerging Contributor
So apparently the attribute "grid_code" in your service is of type ObservableCollection. Try replacing the textbox in the maptip with a DataGrid and bind the ItemsSource to [grid_code].


Thanks. I tried it and it still shows the same thing when I hover  over the graphic
0 Kudos
DanielWalton
Frequent Contributor
Ignore my last post. You're seeing the default template, which means Silverlight can't locate your datatemplate. The only way my code would work is if you're creating the maptip within the usercontrol codebehind. Anywhere else and you will need to add the datatemplate XAML to the application resources, either in App.xaml or one of the merged dictionaries.
0 Kudos
IgressT
Emerging Contributor
Ignore my last post. You're seeing the default template, which means Silverlight can't locate your datatemplate. The only way my code would work is if you're creating the maptip within the usercontrol codebehind. Anywhere else and you will need to add the datatemplate XAML to the application resources, either in App.xaml or one of the merged dictionaries.


I moved the datatemplate to merged dictionary and it worked.  Thanks
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
I have also moved the datatemplate to merged dictionary but did not work. Could you please suggest where exactly to insert the following code? Did you set a tag for the <UserControl>? Thanks.

<UserControl.Resources>
        <DataTemplate x:Key="myLabel" >
            <Border Background="Beige" >
                <StackPanel>
                    <TextBlock Text="{Binding [NAME]}" FontSize="8" Foreground="White" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>
0 Kudos
DominiqueBroux
Esri Frequent Contributor

Could you please suggest where exactly to insert the following code?


If you insert it as a resource of your user control, you have to get by code as a resource of this same userControl:

                MapTip = new ContentControl()
                { ContentTemplate = (DataTemplate)myUserControl.Resources["myLabel"] }
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
Thanks. It works.
0 Kudos