Version

Color Map Elements Individually

Another way of coloring your MapElement objects, aside from using Fill Modes, is to color an element directly. To do this, set the Fill property of the element to the brush that you want to use.

The following code shows you how to conditionally color map elements. In the MapLayer object’s Imported event, property values for each element are checked and an element is colored by setting the Fill property of the element.

In Visual Basic:

' Color Map Elements according to Female to Male ratio
Private Sub statesLayer_Imported(ByVal sender As Object, ByVal e As MapLayerImportEventArgs)
        Dim layer As MapLayer = TryCast(sender, MapLayer)
        ' Declare brushes
        Dim b1 As New SolidColorBrush(Colors.Orange)
        Dim b2 As New SolidColorBrush(Color.FromArgb(255, 30, 144, 255))
        For Each element As MapElement In layer.Elements
                Dim males As Double = CDbl(element.GetProperty("MALES"))
                Dim females As Double = CDbl(element.GetProperty("FEMALES"))
                Dim ratio As Double = Math.Round(females / males, 2)
                element.ToolTip = String.Format("{0:F2}", females / males)
                element.Caption = DirectCast(element.GetProperty("STATE_ABBR"), String)
                ' Check ratio and set color
                If ratio > 1 Then
                        element.Fill = b1
                Else
                        element.Fill = b2
                End If
        Next
End Sub

In C#:

// Color Map Elements according to Female to Male ratio
void statesLayer_Imported(object sender, MapLayerImportEventArgs e)
{
   MapLayer layer = sender as MapLayer;
   // Declare brushes
   SolidColorBrush b1 = new SolidColorBrush(Colors.Orange);
   SolidColorBrush b2 = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255));
   // Check each Map Element
   foreach (MapElement element in layer.Elements)
   {
      double males = (double)element.GetProperty("MALES");
      double females = (double)element.GetProperty("FEMALES");
      double ratio = Math.Round(females / males, 2);
      element.ToolTip = string.Format("{0:F2}", females / males);
      element.Caption = (string)element.GetProperty("STATE_ABBR");
          // Check ratio and set color
      if (ratio > 1)
         element.Fill = b1;
      else
         element.Fill = b2;
   }
}
XamMap Color Individual Map Elements 01.png