MCederholm

Centering a label beneath a marker

Discussion created by MCederholm on Nov 13, 2012
I wanted to create a marker symbol that centers a label beneath the point.  I eventually came up with the following, which works, but I'd like to know if there's a better approach that I'm not seeing, especially one that's pure XAML.  Thanks!

<Window.Resources>
 <src:CanvasLeftMidpointOffsetter x:Key="LeftOffsetter" />
 <esri:MarkerSymbol x:Key="CenterLabelSymbol">
   <esri:MarkerSymbol.ControlTemplate>
    <ControlTemplate>
     <Canvas>
     <Ellipse Canvas.Top="-6" Canvas.Left="-6" Width="12" Height="12" Fill="Black"
       HorizontalAlignment="Center" VerticalAlignment="Center" />
     <TextBlock Canvas.Top="14" 
       HorizontalAlignment="Center" VerticalAlignment="Top" TextAlignment="Center"
       Text="{Binding Attributes[VALUE]}" FontFamily="Arial" FontSize="12">
       <Canvas.Left>
        <Binding Converter="{StaticResource LeftOffsetter}"
          RelativeSource="{x:Static RelativeSource.Self}" Path="ActualWidth" />
       </Canvas.Left>
     </TextBlock>
    </Canvas>
   </ControlTemplate>
 </esri:MarkerSymbol.ControlTemplate>
 </esri:MarkerSymbol>
</Window.Resources>


public class CanvasLeftMidpointOffsetter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      // Convert (double) ActualWidth to (double) Canvas.Left
      double dActualWidth = (double)value;
      return -dActualWidth / 2;
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      // Meaningless
      return 0;
   }
}

Outcomes