Back to Devexpress

How to: Show Tooltips for Map Shapes

wpf-12242-controls-and-libraries-map-control-examples-vector-data-customize-data-appearance-how-to-show-tooltips-for-map-shapes.md

latest8.2 KB
Original Source

How to: Show Tooltips for Map Shapes

  • May 08, 2019
  • 4 minutes to read

This example illustrates how to display tooltips for shapes loaded from Shapefiles ( Countries.dbf , Countries.shp ).

Note that a tooltip displays information (a country name, population) for each map shape from Shapefiles.

To see information (a country name, population) obtained from a Shapefile on a toolltip, specify NAME and POP_EST attributes in the VectorLayerBase.ToolTipPattern property.

Then, enable map tooltips by setting the MapControl.ToolTipEnabled property to true.

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="350" Width="525">
    <Window.Resources>
        <local:MapItemPopulationAttributeToStringTypeConverter x:Key="mapItemPopulationAttributeConverter"/>
        <local:MapItemGdpAttributeToStringTypeConverter x:Key="mapItemGdpAttributeConverter"/>
        <DataTemplate x:Key="tooltipContentTemplate">
            <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}}"
                           Foreground="Gray" FontSize="12"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <dxm:MapControl ToolTipEnabled="True">
            <dxm:VectorLayer ToolTipPattern="{}{NAME}" 
                              ToolTipContentTemplate="{Binding Source={StaticResource tooltipContentTemplate}}">
                <dxm:ShapefileDataAdapter FileUri="/ShowToolTips;component/Data/Shapefiles/Countries.shp"/>
            </dxm:VectorLayer>
        </dxm:MapControl>
    </Grid>
</Window>
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 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

    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
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 attr.Value;
        }

        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();
        }
    }
}

See Also

How to: Show Titles for Map Shapes

How to: Create a Map Item Attribute and Show its Value in the Map Tooltip

How to: Customize the Appearance of a Vector Element

How to: Show a Title for a Vector Element

How to: Customize the Appearance of a Vector Element's Title

How to: Customize the Appearance of a Map Pushpin

How to: Customize Animation of a Map Pushpin

How to: Provide Cylindrical Equal-Area Projections