Back to Devexpress

How to: Implement a Custom Map Tooltip Shape

wpf-117405-controls-and-libraries-map-control-examples-vector-data-customize-data-appearance-how-to-implement-a-custom-map-tooltip-shape.md

latest6.7 KB
Original Source

How to: Implement a Custom Map Tooltip Shape

  • Jun 07, 2019
  • 3 minutes to read

This example demonstrates how to configure a custom form of a map tooltip shape.

To do this, assign a DataTemplate object to the MapControl.ToolTipTemplate property.

xaml
<Window x:Class="ShowToolTips.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"
        xmlns:local="clr-namespace:ShowToolTips"
        Title="MainWindow" Height="700" Width="700">
    <Window.Resources>
        <local:MapItemPopulationAttributeToStringTypeConverter x:Key="mapItemPopulationAttributeConverter"/>
        <local:MapItemGdpAttributeToStringTypeConverter x:Key="mapItemGdpAttributeConverter"/>
        <DataTemplate x:Key="tooltipContentTemplate">
            <Border BorderThickness="1" CornerRadius="10" Opacity="1.0" Background="#FF2C2B2B" >
                <Border.Effect>
                    <DropShadowEffect BlurRadius="10" ShadowDepth="10" Opacity="0.5"/>
                </Border.Effect>
                <StackPanel Orientation="Vertical" Margin="8">
                    <TextBlock Text="{Binding ToolTipText}"
                           Foreground="White" FontSize="24"/>
                    <TextBlock Text="{Binding Item, Converter={StaticResource mapItemGdpAttributeConverter}, StringFormat=GDP: {0:C0}M}"
                           Foreground="Gray" FontSize="12"/>
                    <TextBlock Text="{Binding Item, Converter={StaticResource mapItemPopulationAttributeConverter}, StringFormat=Population: {0}M}"
                           Foreground="Gray" FontSize="12"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Border>
            <dxm:MapControl ToolTipEnabled="True" 
                            ZoomLevel="2" 
                            ToolTipTemplate="{Binding Source={StaticResource tooltipContentTemplate}}">
                <dxm:VectorLayer ToolTipPattern="{}{NAME}">
                    <dxm:ShapefileDataAdapter FileUri="/ShowToolTips;component/Data/Shapefiles/Countries.shp"/>
                </dxm:VectorLayer>
            </dxm:MapControl>
        </Border>
    </Grid>
</Window>
csharp
using DevExpress.Xpf.Map;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace ShowToolTips {
    class MapItemPopulationAttributeToStringTypeConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            ShapeTitle title = value as ShapeTitle;
            MapItem item;
            if (title == null)
                item = value as MapItem;
            else
                item = title.MapShape;

            if (item == null) return null;

            var attr = item.Attributes["POP_EST"];
            if (attr == null) return null;

            return System.Math.Round((double)attr.Value / 1000000, 2);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

    class MapItemGdpAttributeToStringTypeConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            ShapeTitle title = value as ShapeTitle;
            MapItem item;
            if (title == null)
                item = value as MapItem;
            else
                item = title.MapShape;

            if (item == null) return null;

            var attr = item.Attributes["GDP_MD_EST"];
            if (attr == null) return null;

            return attr.Value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
}
vb
Imports DevExpress.Xpf.Map
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Globalization
Imports System.Linq
Imports System.Text
Imports System.Windows.Data

Namespace ShowToolTips
    Friend Class MapItemPopulationAttributeToStringTypeConverter
        Implements IValueConverter

        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
            Dim title As ShapeTitle = TryCast(value, ShapeTitle)
            Dim item As MapItem
            If title Is Nothing Then
                item = TryCast(value, MapItem)
            Else
                item = title.MapShape
            End If

            If item Is Nothing Then
                Return Nothing
            End If

            Dim attr = item.Attributes("POP_EST")
            If attr Is Nothing Then
                Return Nothing
            End If

            Return System.Math.Round(CDbl(attr.Value) / 1000000, 2)
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class

    Friend Class MapItemGdpAttributeToStringTypeConverter
        Implements IValueConverter

        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
            Dim title As ShapeTitle = TryCast(value, ShapeTitle)
            Dim item As MapItem
            If title Is Nothing Then
                item = TryCast(value, MapItem)
            Else
                item = title.MapShape
            End If

            If item Is Nothing Then
                Return Nothing
            End If

            Dim attr = item.Attributes("GDP_MD_EST")
            If attr Is Nothing Then
                Return Nothing
            End If

            Return attr.Value
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class
End Namespace