Back to Devexpress

MapControl.DrawMapItem Event

windowsforms-devexpress-dot-xtramap-dot-mapcontrol-0b1432b0.md

latest8.8 KB
Original Source

MapControl.DrawMapItem Event

Provides the capability to custom paint map items.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public event DrawMapItemEventHandler DrawMapItem
vb
Public Event DrawMapItem As DrawMapItemEventHandler

Event Data

The DrawMapItem event's data class is DrawMapItemEventArgs. The following properties provide information specific to this event:

PropertyDescription
FillGets or sets the color that is used to fill a map item when the MapControl.DrawMapItem event is fired.
IsHighlightedGets a value that indicates whether a map item is highlighted.
IsSelectedGets a value that indicates whether a map item is selected.
ItemGets a map item to be represented in the map control. Inherited from MapItemEventArgs.
LayerGets an object representing a map items layer when handling the MapControl.DrawMapItem event.
StrokeGets or sets the Color that specifies how the map item outline is painted.
StrokeWidthGets or sets a value that specifies the width of the stroke on the current map item.

Remarks

Use the DrawMapItem event to create a custom appearance for map items.

Example

This example illustrates how to customize vector items when they are drawn on a map.

To do this, it’s necessary to handle the MapControl.DrawMapItem event and provide new values for the event arguments object.

csharp
using DevExpress.XtraMap;

namespace DrawMapItemExample {

    public class MapItemFactory : DefaultMapItemFactory {

        protected override void InitializeItem(MapItem item, object obj) {
            base.InitializeItem(item, obj);
            MapRectangle rect = item as MapRectangle;
            if(rect != null) {
                rect.Width = 1000;
                rect.Height = 1000;
            }
        }
    }
}
csharp
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace DrawMapItemExample {

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();

            mapControl1.SetMapItemFactory(new MapItemFactory());
            ListSourceDataAdapter adapter = (ListSourceDataAdapter)Layer.Data;
            adapter.DataSource = TestData.Instance;
        }

        public VectorItemsLayer Layer { get { return (VectorItemsLayer)mapControl1.Layers[0]; } }

        private void mapControl1_DrawMapItem(object sender, DrawMapItemEventArgs e) {
            Color color1 = (Color)e.Item.Attributes["ColorProp"].Value;
            Color color2 = Color.FromArgb(255 - color1.R, 255 - color1.G, 255 - color1.B);
            e.Fill = e.IsHighlighted ? color2 : color1;
            e.Stroke = e.IsHighlighted ? color1 : color2;
            e.StrokeWidth = e.IsHighlighted ? 5 : 2;
        }
    }

}
csharp
using System.Collections.Generic;
using System.Drawing;

namespace DrawMapItemExample {

    public class TestDataItem {
        public string Label { get; set; }
        public double Lat { get; set; }
        public double Lon { get; set; }
        public Color Tag { get; set; }
    }

    public class TestData : List<TestDataItem> {
        static readonly TestData instance;

        static TestData() {
            instance = new TestData();
            instance.Add(new TestDataItem() { Lat = 14, Lon = 8, Label = "A", Tag = Color.Yellow });
            instance.Add(new TestDataItem() { Lat = 4, Lon = -2, Label = "B", Tag = Color.Purple });
            instance.Add(new TestDataItem() { Lat = -6, Lon = -12, Label = "C", Tag = Color.Red });
        }

        public static TestData Instance { get { return instance; } }
    }

}
vb
Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace DrawMapItemExample

    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()

            mapControl1.SetMapItemFactory(New MapItemFactory())
            Dim adapter As ListSourceDataAdapter = CType(Layer.Data, ListSourceDataAdapter)
            adapter.DataSource = TestData.Instance
        End Sub

        Public ReadOnly Property Layer() As VectorItemsLayer
            Get
                Return CType(mapControl1.Layers(0), VectorItemsLayer)
            End Get
        End Property

        Private Sub mapControl1_DrawMapItem(ByVal sender As Object, ByVal e As DrawMapItemEventArgs) Handles mapControl1.DrawMapItem
            Dim color1 As Color = CType(e.Item.Attributes("ColorProp").Value, Color)
            Dim color2 As Color = Color.FromArgb(255 - color1.R, 255 - color1.G, 255 - color1.B)
            e.Fill = If(e.IsHighlighted, color2, color1)
            e.Stroke = If(e.IsHighlighted, color1, color2)
            e.StrokeWidth = If(e.IsHighlighted, 5, 2)
        End Sub
    End Class

End Namespace
vb
Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.XtraMap

Namespace DrawMapItemExample

    Public Class MapItemFactory
        Inherits DefaultMapItemFactory

        Protected Overrides Sub InitializeItem(ByVal item As MapItem, ByVal obj As Object)
            MyBase.InitializeItem(item, obj)
            Dim rect As MapRectangle = TryCast(item, MapRectangle)
            If rect IsNot Nothing Then
                rect.Width = 1000
                rect.Height = 1000
            End If
        End Sub
    End Class
End Namespace
vb
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Drawing

Namespace DrawMapItemExample

    Public Class TestDataItem
        Private privateLabel As String
        Public Property Label() As String
            Get
                Return privateLabel
            End Get
            Set(ByVal value As String)
                privateLabel = value
            End Set
        End Property
        Private privateLat As Double
        Public Property Lat() As Double
            Get
                Return privateLat
            End Get
            Set(ByVal value As Double)
                privateLat = value
            End Set
        End Property
        Private privateLon As Double
        Public Property Lon() As Double
            Get
                Return privateLon
            End Get
            Set(ByVal value As Double)
                privateLon = value
            End Set
        End Property
        Private privateTag As Color
        Public Property Tag() As Color
            Get
                Return privateTag
            End Get
            Set(ByVal value As Color)
                privateTag = value
            End Set
        End Property
    End Class

    Public Class TestData
        Inherits List(Of TestDataItem)
        Private Shared ReadOnly instance_Renamed As TestData

        Shared Sub New()
            instance_Renamed = New TestData()
            instance_Renamed.Add(New TestDataItem() With {.Lat = 14, .Lon = 8, .Label = "A", .Tag = Color.Yellow})
            instance_Renamed.Add(New TestDataItem() With {.Lat = 4, .Lon = -2, .Label = "B", .Tag = Color.Purple})
            instance_Renamed.Add(New TestDataItem() With {.Lat = -6, .Lon = -12, .Label = "C", .Tag = Color.Red})
        End Sub

        Public Shared ReadOnly Property Instance() As TestData
            Get
                Return instance_Renamed
            End Get
        End Property
    End Class

End Namespace

See Also

MapControl Class

MapControl Members

DevExpress.XtraMap Namespace