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

    Hi,
    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">
                <telerik:MapPolygon.CaptionTemplate>
                    <DataTemplate>
                        <Grid Background="Yellow">
                            <telerik:MapLayer.HotSpot="0.50.5">
                                <TextBlock Text="My Custom Text" />
                        </Grid>
                    </DataTemplate>
                </telerik:MapPolygon.CaptionTemplate>
            </telerik:MapPolygon>

    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
    Admin
    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()
            {
                InitializeComponent();
     
                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");
                this.informationLayer.Items.Add(polygon);
            }
     
            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=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\""
                    + " xmlns:telerik=\"http://schemas.telerik.com/2008/xaml/presentation\">"
                    + " <Grid Background=\"Yellow\" telerik:MapLayer.HotSpot=\"0.5, 0.5\">"
                    + xTextBlock.ToString(SaveOptions.DisableFormatting)
                    + " </Grid>"
                    + "</DataTemplate>") as DataTemplate;
     
                polygon.CaptionTemplate = template;
     
                return polygon;
            }
        }
    }

    Greetings,
    Andrey Murzov
    the Telerik team

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

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top