Select to view content in your preferred language

MapTips onm graphicsLayer By Code

3028
10
05-05-2011 05:46 AM
MarcoRosa
Emerging Contributor
Hi guys,
i would like to replicate this xaml section code by behind code:


<esri:GraphicsLayer ID="GeocodeResultsGraphicsLayer" >
<esri:GraphicsLayer.MapTip>
  <Grid>
   <Border BorderBrush="Black" CornerRadius="10"  BorderThickness="1" Background="#FFFFE300" />
     <StackPanel Orientation="Vertical" Margin="5">
       <TextBlock x:Name="LocatorText" Text ="{Binding [DisplayName]}" HorizontalAlignment="Left" />
     </StackPanel>
  </Grid>
</esri:GraphicsLayer.MapTip>
</esri:GraphicsLayer>
            
my behind code is that:

GraphicsLayer graphicsLayer = new GraphicsLayer();

TextBox t = new TextBox();
System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
binding.Source = graphicsLayer;
binding.Path = new PropertyPath("[DisplayName]");
t.SetBinding(TextBox.TextProperty, binding);
           
Border border = new Border();
border.BorderThickness = new Thickness(1);
Panel panel = new StackPanel();
panel.Children.Add(t);
border.Child = panel;
           
graphicsLayer.ID = "GeocodeResultsGraphicsLayer";
graphicsLayer.MapTip = border;

..... later in graphicslayer ...

graphic.Attributes.Add("DisplayName", geocodeResult.DisplayName);
graphicsLayer.Graphics.Add(graphic);


Now when i put mouse over graphic objects in graphicsLayer i have no errors but is displayed a maptip whitout name.
I've tried also
binding.Path = new PropertyPath("Attributes[DisplayName]");
but nothing

where's the error ?
Thanks a lot
GD
0 Kudos
10 Replies
JenniferNery
Esri Regular Contributor
This is a sample:

XAML-code
<esri:GraphicsLayer.MapTip>
  <TextBlock Text="{Binding [Location], FallbackValue=BindingFailed}"/>
</esri:GraphicsLayer.MapTip>


Equivalent code-behind:
private void GraphicsLayer_Initialized(object sender, EventArgs e)
{
 GraphicsLayer l = sender as GraphicsLayer;
 TextBlock tb = new TextBlock();
 Binding b = new Binding("[Location]");
 tb.SetBinding(TextBlock.TextProperty, b);
 l.MapTip = tb;
 foreach (var g in l.Graphics)
  g.Attributes["Location"] = g.Geometry;
}


Your code was close except you need not set Binding.Source because this only overwrites the DataContext that will be set for your MapTip. The MapTip should be pointing to the Graphic.Attributes not GraphicsLayer.
0 Kudos
PaulHuppé
Deactivated User
Hi,

I am also trying to define maptips from code behind.  Jennifer, looking at your response, it seems to assume that you have created the graphics layer in XAML.  I need to define everything in code-behind, the graphics layer, the template to display the maptip, the service to get the data from and getting the fields/data to display.

Paul
0 Kudos
PaulHuppé
Deactivated User
Hi,

Maybe this is not enough information since there have been no replies yet.


I am also trying to define maptips from code behind.  Jennifer, looking at your response, it seems to assume that you have created the graphics layer in XAML.  I need to define everything in code-behind, the graphics layer, the template to display the maptip, the service to get the data from and getting the fields/data to display.


What we need is to dynamically create maptip graphic layers (no XAML) from code-behind after the map has loaded.

Paul
0 Kudos
JenniferNery
Esri Regular Contributor
Kindly see the equivalent code-behind in Post#2. This creates TextBlock with Binding in code and then assigns this control to GraphicsLayer.Maptip. I hope that putting XAML-code above it does not confuse you. I'm only showing how those two codes are equivalent. You can choose one over the other and still get the same result.
0 Kudos
PaulHuppé
Deactivated User
Hi Jennifer,

No, I was not confused by the XAML above the code-behind.  I wanted to know the steps required to do everything in code-behind, including setting up the graphics layer. Would it be something like:

Option 1:

  1. Define a new esri:MapTip control

  2. Add the control to the layout

  3. Create a query task for the layer desired


or Option 2:

  1. Create a new esri:GraphicsLayer

  2. Define the GraphicsLayer.Maptip template

  3. Create the query task for the layer


Paul
0 Kudos
JenniferNery
Esri Regular Contributor
This is the only code you will need if you want to do everything in code-behind:
1. Get the GraphicsLayer.
2. Create contents of your map tip with binding (this sample uses TextBlock where Text binds to Graphic Attribute Location).
3. Assign GraphicsLayer.MapTip.
GraphicsLayer l = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
TextBlock tb = new TextBlock();
Binding b = new Binding("[Location]");
tb.SetBinding(TextBlock.TextProperty, b);
l.MapTip = tb;


I suppose, your QueryTask_ExecuteCompleted event is where you get the graphics. It can already contain the graphic.Attribute["Location"] (when Query.OutFields include this attribute) or maybe it's not a field in your service so this is the time you can set it.
void QueryTask_ExecuteCompleted(object sender, QueryEventArgs e)
{
 foreach (var g in e.FeatureSet.Features)
  g.Attributes["Location"] = g.Geometry; // if not in the OutFields.
}
0 Kudos
PaulHuppé
Deactivated User
Thanks Jennifer.
Paul
0 Kudos
AndrewMurdoch
Regular Contributor
Jennifer and Paul,
I got your code to work presenting a MapTip textblock through code.  Thanks for that!  However, I'm trying to use the MVVM model and while I could construct fancy display code in code behind, that seems to defeat the purpose of MVVM. 

Is there any way to create the GraphicsLayer and MapTip in code behind (which I've done already) and bind it to a XAML template?

Thanks for your help.
Andrew
0 Kudos
PaulHuppé
Deactivated User
Hi Andrew,

I am finding the same kinds of difficulties as well because we are using MVVM also.  I am new to MVVM, so until I get more experience with it, I have to struggle to get things working like they do in regular code behind.

I also am struggling with templates.  On legend refresh event in code behind, I could set a Tag attribute on the LegendItemViewModel which would define the template key name of a template as a resource.  But, trying the same code in the ViewModel, it tells me that a LegendItemViewModel item does not have a Tag attribute!!  Things like that are fustrating, so I share your pain!

Paul
0 Kudos