Back to Devexpress

How to: Customize the Appearance of Vector Elements

windowsforms-16958-controls-and-libraries-map-control-examples-vector-data-customize-data-appearance-how-to-customize-the-appearance-of-vector-elements.md

latest6.6 KB
Original Source

How to: Customize the Appearance of Vector Elements

  • Nov 13, 2018
  • 4 minutes to read

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