How to show a text over mappolygon at runtime

2 posts, 0 answers
  1. Vishal
    Vishal avatar
    16 posts
    Member since:
    Jan 2011

    Posted 12 Oct 2011 Link to this post

    My requirement is to show the text over the map polygon at runtime.
    From the xaml we can do this like:
    <telerik:MapPolygon Points="56,-100 56,-108 48,-108 48,-100" Fill="Green" Stroke="Red"
                        StrokeThickness="4" CaptionLocation="52,-104">
                        <Grid Background="Yellow">
                                <TextBlock Text="My Custom Text" />

    But from the code behind how we can do the same thing.

    MapPolygon CreatePolygon(IEnumerable<Location> points, string areaName)
                var polygon = new MapPolygon();
                polygon.Points = this.pins.Points;
                polygon.ToolTip = areaName;
                // also I want to set the area name over the MapPolygon
                return polygon;
  2. Andrey
    Andrey avatar
    1681 posts

    Posted 17 Oct 2011 Link to this post

    Hello Vishal,

    You can use the XamlReader.Parse method from the code behind for building the data template for the MapPolygon.CaptionTemplate property.
    The sample code is below.
    using System;
    using System.Windows;
    using System.Windows.Markup;
    using System.Windows.Media;
    using System.Xml.Linq;
    using Telerik.Windows.Controls.Map;
    namespace CaptionFromCodeBehind
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
            public MainWindow()
                var collection = new LocationCollection();
                collection.Add(new Location(56, -100));
                collection.Add(new Location(56, -108));
                collection.Add(new Location(48, -108));
                collection.Add(new Location(48, -100));
                var polygon = this.CreatePolygon(collection, "Area Name");
            private MapPolygon CreatePolygon(LocationCollection points, string areaName)
                var polygon = new MapPolygon();
                polygon.ToolTip = areaName;
                polygon.Points = points;
                polygon.Fill = new SolidColorBrush(Colors.Green);
                polygon.Stroke = new SolidColorBrush(Colors.Red);
                polygon.StrokeThickness = 4;
                polygon.CaptionLocation = new Location(52, -104);
                // set the caption template
                XElement xTextBlock = new XElement("TextBlock");
                xTextBlock.SetAttributeValue("Text", areaName);
                DataTemplate template = XamlReader.Parse(""
                    + "<DataTemplate xmlns=\"\""
                    + " xmlns:telerik=\"\">"
                    + " <Grid Background=\"Yellow\" telerik:MapLayer.HotSpot=\"0.5, 0.5\">"
                    + xTextBlock.ToString(SaveOptions.DisableFormatting)
                    + " </Grid>"
                    + "</DataTemplate>") as DataTemplate;
                polygon.CaptionTemplate = template;
                return polygon;

    Andrey Murzov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top