Format the Text Displayed by the MapMouseLocationIndicator Element
Environment
Product Version | 2025.3.815 |
Product | RadMap for WPF |
Description
Format the text that is displayed by the MapMouseLocationIndicator
control.
Solution
To format the text, extend the MapMouseLocationIndicator class to define a TextBlock
property, which will call the GetTemplateChild
method with parameter "PART_TextBlock", and override the OnMapInitialize
method. In it, you can format the text, as well as subscribe to the MouseMove
event of the RadMap
control that comes as a parameter, in which you can format the TextBlock
each time the mouse moves.
Extending the MapMouseLocationIndicator class to customize the format
public class CustomMapMouseLocationIndicator : MapMouseLocationIndicator
{
private const string IndicatorTextBlockPartName = "PART_TextBlock";
private TextBlock IndicatorTextBlock
{
get
{
return this.GetTemplateChild(IndicatorTextBlockPartName) as TextBlock;
}
}
protected override void OnMapInitialize(RadMap oldMapControl, RadMap newMapControl)
{
if (oldMapControl != null)
{
oldMapControl.MouseMove -= new MouseEventHandler(this.MapControl_MouseMove);
}
if (newMapControl != null)
{
newMapControl.MouseMove += new MouseEventHandler(this.MapControl_MouseMove);
if (this.IndicatorTextBlock != null)
{
this.IndicatorTextBlock.Text = this.LocationToString(newMapControl.Center);
}
}
}
private void MapControl_MouseMove(object sender, MouseEventArgs e)
{
Location mouse = Location.GetCoordinates(this.MapControl, e.GetPosition(this.MapControl));
if (this.IndicatorTextBlock != null)
{
this.IndicatorTextBlock.Text = this.LocationToString(mouse);
}
}
private string LocationToString(Location location)
{
string locationStr = string.Empty;
switch (this.LocationFormat)
{
case LocationFormat.Geographical:
LocationDegrees degrees = new LocationDegrees(location);
locationStr = degrees.ToString();
break;
case LocationFormat.Number:
locationStr = $"{location.Latitude:n5}, {location.Longitude:n5}";
break;
}
return locationStr;
}
}
Using the extended MapMouseLocationIndicator class
If you are using the NoXaml version of our assemblies, you need to create a new
Style
that targets theCustomMapMouseLocationIndicator
type. Then, it needs to be based via theBasedOn
property on the defaultStyle
for theMapMouseLocationIndicator
type, which has an x:Key="MapMouseLocationIndicatorStyle".
MapMouseLocationIndicator with formatted text