Select to view content in your preferred language

Dynamic Maptip binding

846
3
Jump to solution
02-10-2013 06:00 PM
PhilipKnight1
Regular Contributor
I am trying to make the maptip's text it displays be dynamically created. What I mean by this specifically is hoe do I make it so that I can choose at runtime what properties from the specific graphic will be displayed.

I'm able to hardcode which fields will be displayed via XAML (e.g. MAINWORKCENTERDESCR & NOTIFICATIONPRIORITY):
            <esri:SimpleMarkerSymbol x:Key="BlueMarkerSymbol" Color="Blue" Size="10" Style="Circle">                 <esri:SimpleMarkerSymbol.ControlTemplate>                     <ControlTemplate>                         <Grid>                             <Ellipse Fill="{Binding Symbol.Color}" Width="{Binding Symbol.Size}"                                  Height="{Binding Symbol.Size}" Stroke="Black" StrokeThickness="0.3" />                              <StackPanel Margin="5">                                 <TextBlock Text="{Binding [MAINWORKCENTERDESCR]}" FontWeight="Bold" />                                 <StackPanel Orientation="Horizontal">                                     <TextBlock Text="Priority: " />                                     <TextBlock Text="{Binding [NOTIFICATIONPRIORITY]}" />                                 </StackPanel>                             </StackPanel>                             <Border BorderBrush="Black" BorderThickness="1" CornerRadius="2" />                         </Grid>                     </ControlTemplate>                 </esri:SimpleMarkerSymbol.ControlTemplate>             </esri:SimpleMarkerSymbol>


How can I accomplish this in code behind, so that I can set the fields I would like displayed?
0 Kudos
1 Solution

Accepted Solutions
PhilipKnight1
Regular Contributor
I wasn't abel to get the data binding to work, but because of the post I was able to realize I could just create the maptips for each individual graphic, though I wonder if by doing that it's less effecient... oh well. Here's what I did:


I created a function to create the mapTip here

        private FrameworkElement getMapTip(Graphic graphic)         {             Grid grid = new Grid();             grid.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));             grid.Margin = new Thickness(2);              //create outer stack panel             StackPanel outerStackPanel = new StackPanel();             outerStackPanel.Margin = new Thickness(5);              //add border to grid             Border border = new Border();             border.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));             border.CornerRadius = new CornerRadius(2);             border.Child = outerStackPanel;     //put border around outer stack panel              foreach (string attribute in _mapTipAttributes)             {                 TextBlock txtBlock = new TextBlock();                  txtBlock.Text = attribute + ": " + graphic.Attributes[attribute];                 outerStackPanel.Children.Add(txtBlock);             }              grid.Children.Add(border);              return grid;         }


and I call it where I create my graphics:

        void queryTask_ExecuteCompleted(object sender, QueryEventArgs args)         {             FeatureSet featureSet = args.FeatureSet;              if (featureSet == null || featureSet.Features.Count < 1)             {                 MessageBox.Show("No features returned from query");                 return;             }              GraphicsLayer graphicsLayer = _map.Layers["NotifLayer"] as GraphicsLayer;             graphicsLayer.ClearGraphics();              foreach (Graphic graphic in featureSet.Features)             {                 graphic.Symbol = LayoutRoot.Resources["YellowMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;                 graphic.MapTip = getMapTip(graphic);                 graphicsLayer.Graphics.Add(graphic);             }         }

View solution in original post

0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
Likely there are lots of possibilities. One option is to first define your maptip as an ItemsControl allowing to display a list of attributes.
Something like this (for example with a datagrid):
<esri:FeatureLayer.MapTip>
    <sdk:DataGrid ItemsSource="{Binding}" HeadersVisibility="None"/>
</esri:FeatureLayer.MapTip>


Then you could develop a value converter taking an attributes dictionary as input and returning a dictionary with the attributes you want to display (how you filter the attributes is depending on your application).

Then you can use this converter in XAML:

<esri:FeatureLayer.MapTip>
    <sdk:DataGrid ItemsSource="{Binding Converter={StaticResource myDictionaryFilterConverter}}" HeadersVisibility="None"/>
</esri:FeatureLayer.MapTip>


Hope this helps.
0 Kudos
PhilipKnight1
Regular Contributor
Likely there are lots of possibilities. One option is to first define your maptip as an ItemsControl allowing to display a list of attributes.
Something like this (for example with a datagrid):
<esri:FeatureLayer.MapTip>
    <sdk:DataGrid ItemsSource="{Binding}" HeadersVisibility="None"/>
</esri:FeatureLayer.MapTip>


Then you could develop a value converter taking an attributes dictionary as input and returning a dictionary with the attributes you want to display (how you filter the attributes is depending on your application).

Then you can use this converter in XAML:

<esri:FeatureLayer.MapTip>
    <sdk:DataGrid ItemsSource="{Binding Converter={StaticResource myDictionaryFilterConverter}}" HeadersVisibility="None"/>
</esri:FeatureLayer.MapTip>


Hope this helps.


Where do I put that XAML though? My graphics layer is defined in code? Will I have to move it to XAML? Or can I define the binding in the code behind?
0 Kudos
PhilipKnight1
Regular Contributor
I wasn't abel to get the data binding to work, but because of the post I was able to realize I could just create the maptips for each individual graphic, though I wonder if by doing that it's less effecient... oh well. Here's what I did:


I created a function to create the mapTip here

        private FrameworkElement getMapTip(Graphic graphic)         {             Grid grid = new Grid();             grid.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));             grid.Margin = new Thickness(2);              //create outer stack panel             StackPanel outerStackPanel = new StackPanel();             outerStackPanel.Margin = new Thickness(5);              //add border to grid             Border border = new Border();             border.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));             border.CornerRadius = new CornerRadius(2);             border.Child = outerStackPanel;     //put border around outer stack panel              foreach (string attribute in _mapTipAttributes)             {                 TextBlock txtBlock = new TextBlock();                  txtBlock.Text = attribute + ": " + graphic.Attributes[attribute];                 outerStackPanel.Children.Add(txtBlock);             }              grid.Children.Add(border);              return grid;         }


and I call it where I create my graphics:

        void queryTask_ExecuteCompleted(object sender, QueryEventArgs args)         {             FeatureSet featureSet = args.FeatureSet;              if (featureSet == null || featureSet.Features.Count < 1)             {                 MessageBox.Show("No features returned from query");                 return;             }              GraphicsLayer graphicsLayer = _map.Layers["NotifLayer"] as GraphicsLayer;             graphicsLayer.ClearGraphics();              foreach (Graphic graphic in featureSet.Features)             {                 graphic.Symbol = LayoutRoot.Resources["YellowMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;                 graphic.MapTip = getMapTip(graphic);                 graphicsLayer.Graphics.Add(graphic);             }         }
0 Kudos