Mapping in WPF
Written by David Conrad   
Tuesday, 11 January 2011
Article Index
Mapping in WPF
Map navigation
Custom data
Doing more with data

Banner

Custom data

All of the data that is in the data file is read in and stored in each of the corresponding MapElements using the same name. Any that you explicitly map to intrinsic properties such as Caption are not mapped to new properties. The point is that every MapElement has all of the data that the database defines stored within it and you can programmatically make use of it.

Before we continue it is worth pointing out that you can't access the element collection before the Shapefile has finished loading. What this means is that you generally have to use the layer imported event handler to do the job. In our example we simply use a button which you can click after the map has displayed to process the element collection.

For example, to  store the population figures in the Caption property programmatically you would write something like:

foreach (MapElement ele 
in WorldLayer.Elements)
{
ele.Caption = ele.GetProperty(
"POP_CNTRY").ToString();
}

If we are going to reference WorldLayer in a button click event handler then we need it to be a global variable so add:

MapLayer WorldLayer;

and change the line that creates the new MapLayer to:

WorldLayer = new MapLayer();

Notice the way that you can use the MapLayer's Element collection to step though each of the elements and the GetProperty to retrieve any property generated by the database.

You can, of course apply formatting to the data before assigning it to the display property. For example:

foreach (MapElement ele 
in WorldLayer.Elements)
{
double pop = (double) ele.
GetProperty("POP_CNTRY");
ele.Caption = String.Format(
"{0:0,0.0}", pop);
}

formats the population number displayed to have comma thousands separators.

Data on the fly

You can also create the data you want to display on the fly. You can do this is two ways - either by creating a custom property or by using one of the existing properties.

For example:

Random R=new Random();
foreach (MapElement ele
in WorldLayer.Elements)
{
ele.SetProperty("MyData",
R.NextDouble());
ele.Caption = ele.GetProperty(
"MyData").ToString();
}

In this case we create a new MyData property and set it to a random double and then set the existing Caption property to it. Notice that all properties we create are of type object and its up to you to keep track of what they are and apply appropriate casts. Also notice that we could have done the same job simply by assigning a random value to Caption.

You can set the color of elements based on data using the Fill property and you can automatically map values to colors using the Fillmode and optionally the Color Swatch Pane.

Banner



Last Updated ( Tuesday, 11 January 2011 )