Select to view content in your preferred language

GraphicsLayer.MapTips code behind using XAML user control

2458
1
Jump to solution
10-16-2012 10:49 AM
CameronBlandy
Regular Contributor
I have created a user control (template) to use for a MapTip that I am adding to a GraphicsLayer. The Graphics Layer and Map Tips are defined in code.

             <Border x:Name="MapTipBorder" esri:GraphicsLayer.MapTipHideDelay="00:00:01" CornerRadius="10" BorderBrush="#FF222957" BorderThickness="3" Margin="0,0,15,15">                 <Border.Background>                     <LinearGradientBrush EndPoint="1.038,1.136" StartPoint="0.015,0.188">                         <GradientStop Color="#FFD1DFF2"/>                         <GradientStop Color="#FF092959" Offset="0.946"/>                     </LinearGradientBrush>                 </Border.Background>                 <Border.Effect>                     <DropShadowEffect ShadowDepth="10" BlurRadius="14" Direction="300" />                 </Border.Effect>                 <StackPanel Orientation="Vertical" Margin="20,15,20,15">                     <StackPanel Orientation="Horizontal" Margin="0,0,0,0">                         <TextBlock Text="Beacon ID: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="12" />                         <TextBlock Text="{Binding [BEACONID]}" Foreground="#FFFFFFFF" FontSize="12" FontStyle="Italic" FontFamily="Portable User Interface" />                     </StackPanel>                     <StackPanel Orientation="Horizontal">                         <TextBlock Text="Vehicle Name: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="12" />                         <TextBlock Text="{Binding [VEHICLENAME]}" Foreground="#FFFFFFFF" FontSize="12" FontStyle="Italic" FontFamily="Portable User Interface" />                     </StackPanel>                     <StackPanel Orientation="Horizontal">                         <TextBlock Text="Date and Time: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="12" />                         <TextBlock Text="{Binding [TIMESTAMP]}" Foreground="#FFFFFFFF" FontSize="12" FontStyle="Italic" FontFamily="Portable User Interface" />                     </StackPanel>                     <StackPanel Orientation="Horizontal">                         <TextBlock Text="Speed: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="12" />                         <TextBlock Text="{Binding [SPEED]}" Foreground="#FFFFFFFF" FontSize="12" FontStyle="Italic" FontFamily="Portable User Interface" />                     </StackPanel>                 </StackPanel>             </Border>


I want to use the XAML above in code to show the MapTips. When I do the following in code the application crashes when I hover over the graphic because the MapTips are null (I think).
The errror in the immediate wiondow in VS is:
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll

 Graphic graphic = new Graphic();             graphic.Geometry = UnitLocation; //defined elsewhere             _activeSymbol =  (ESRI.ArcGIS.Client.Symbols.Symbol)LayoutRoot.Resources["GreenDotMarkerSymbol"];             graphic.Symbol = _activeSymbol;             graphic.Attributes.Add("BEACONID", obj.beaconId);             graphic.Attributes.Add("VEHICLENAME", obj.unitName);             DateTime timestamp = obj.timestamp;             graphic.Attributes.Add("TIMESTAMP", timestamp.AddHours(1)); //to correct for DST             graphic.Attributes.Add("SPEED", obj.speed);             graphic.MapTip = (FrameworkElement)LayoutRoot.Resources["MapTipBorder"];             graphic.MapTip.DataContext = graphic.Attributes; // have tried with and without this line.             _serviceCrewLayer.Graphics.Add(graphic);


Does anybody have any idea why this would not work?

Thanks,
Cameron
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
Declaring the maptip in the resources is not working, becasue the UIElement is not shareable.

You can either create your own maptip control and instantiate it by code, or declare the datatemplate in the resources and instantiate a ContentControl by code (see this thread).

View solution in original post

0 Kudos
1 Reply
DominiqueBroux
Esri Frequent Contributor
Declaring the maptip in the resources is not working, becasue the UIElement is not shareable.

You can either create your own maptip control and instantiate it by code, or declare the datatemplate in the resources and instantiate a ContentControl by code (see this thread).
0 Kudos