Bing Maps on WPF and custom PushPin tutorial for PixelSense

I’ve just came back from a trip to Washington DC, where I’ve been at the 50th IAM International Moving Annual Meeting. This was for my employer TellUs, who’s into lead generation for international removal companies. A lot of people asked for the reason why I went to the states, this is it. And of course because we bought a Microsoft PixelSense table which we used to visualize our data on.

The table had to ship however a little before we could finish the applications. After installing them at the convention, everything worked flawlessly. Well, until our sales representatives started working with it. We build a map with thousands of clustered pushpins on it. But there was a problem.

  • The push pins did not rotate when sales reps rotated the map!!!
    For some reason we did not test rotation of the map on our desktop monitors. Go figure! 😉

This blogpost is how I solved this issue.

Creating the map and adding pushpins
At the convention, we loaded data and placed push pins on the map. For this tutorial we’ll add them via the double-click event of your mouse. Rotation of the map will occur via clicking on the push pins. The push pins will rotate themselves according to rotation of the map. This tutorial was writing in Visual Studio 2010, because Visual Studio 2012 doesn’t support development for PixelSense (yet). If you are completely unfamiliar with adding push pins to a WPF Bing map, check out this tutorial on MSDN.

First, create a new project by pressing CTRL+SHIFT+N and select “WPF Application” under the “Windows” category. You should end up with an almost empty XAML file with a grid in it. Let’s first add the map to it. First add a reference to the Bing assembly that has all the controls. It should be located in one of the following folders

  • C:Program FilesBing Maps WPF ControlV1LibrariesMicrosoft.Maps.MapControl.WPF.dll
  • C:Program Files (x86)Bing Maps WPF ControlV1LibrariesMicrosoft.Maps.MapControl.WPF.dll

After having added the reference, add the namespace to the MainWindow.xaml and add the map. You should end up with the following code.

<Window x:Class="WpfApplication2.MainWindow"
                Title="MainWindow" Height="350" Width="525">
        <m:Map x:Name="myMap" CredentialsProvider="PutYourOwnKeyHere-ItShouldBeVeryLong" />

The CredentialsProvider should have a key that you’ve requested on this page or follow the short tutorial on this page. After adding the key you can already run the application and be able to zoom and pan the map.

Adding PushPins from code

As you can see in the above code, I’ve already given my map a name via x:Name attribute. This way I can access my map from code-behind. This way I can set up my code for the double-click event on the map so I can add a PushPin on every double-click by the user. We’ll quickly run through the code, which shouldn’t be very complex.

public partial class MainWindow : Window
    int counter = 0;

    public MainWindow()

        myMap.MouseDoubleClick += new MouseButtonEventHandler(myMap_MouseDoubleClick);

    private void myMap_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        e.Handled = true;

        Point mousePosition = e.GetPosition(this);
        Location pinLocation = myMap.ViewportPointToLocation(mousePosition);

        Pushpin pin = new Pushpin();
        pin.Location = pinLocation;
        pin.Content = counter += 10;
        pin.MouseDown += new MouseButtonEventHandler(pin_MouseDown);


    private void pin_MouseDown(object sender, MouseButtonEventArgs e)

On line 3 you see a counter variable, which we’ll use to add unique push pins to our map. In the constructor MainWindow() you can see that we set up the handler for the double-click event. This is standard .NET code. In the myMap_MouseDoubleClick event handler, we first set e.Handled to true. Otherwise the map control will try to handle the click itself as well, making it zoom-in every time we add a push pin.

In the next two lines we try to retrieve the exact location where we clicked. This can all be viewed in the MSDN documentation and is standard behavior. Then we instantiate a new Pushpin object, give it the location we just ‘calculated’ and add the value of the counter variable to it as content. As you can see we’ve also already set up the event handler for when we press the pushpin itself.

At line 24 we add the Pushpin to the map so that it will be shown. You can now run this application and start double clicking on the map yourself.
<p align="center"><img style="padding-left: 0px; padding-right: 0px; padding-top: 0px; border-width: 0px;" title="map1" alt="map1" src="/wp-content/uploads/sites/2/2014/01/5001_map1_5f00_thumb_5f00_5b082fd2.png" width="400" height="267" border="0" /></p>
<strong>Rotating the map &amp; Pushpins

</strong>We will now rotate the map as a reaction of the Pushpin that is being clicked by the user. Edit the event handler for the Pushpin click as follows

private void pin_MouseDown(object sender, MouseButtonEventArgs e)
    e.Handled = true;

    var pushPinContent = 0;
    var pushPin = sender as Pushpin;
    if (pushPin != null && pushPin.GetType() == typeof(Pushpin))
        pushPinContent = Convert.ToInt32(pushPin.Content);

    myMap.Heading = (double)pushPinContent;

As you can see we set the event as being handled again at line 3. The sender of the event should be a Pushpin, so we verify this first and then try to obtain the value of the content of the pin itself. After that, we set the heading of the map, which is the rotation. If you now run the application and start clicking the Pushpins (after adding a few), the map should rotate, but the Pushpins should not.

We can easily fix this by binding the heading property of the Pushpin itself, to the heading of the map control. This can be done by the following code, which should be placed directly before we add the Pushpin to the map control.

Binding binding = new Binding();
binding.Source = myMap;
binding.Path = new PropertyPath("Heading");
binding.Mode = BindingMode.OneWay;
pin.SetBinding(Pushpin.HeadingProperty, binding);


Customizing the Pushpin

We will now start customizing the Pushpin to make it show completely different from the default Pushpins. For this we need to create a ControlTemplate in the xaml file. First add a section for static resources in the root object, the Window. Then add the template for the Pushpin. In your existing xaml file, just place the following code directly before the <grid> element.

    <ControlTemplate x:Key="CutomPushpinTemplate" TargetType="m:Pushpin">
        <Grid x:Name="ContentGrid" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Grid Margin="0" Width="33" Height="33">
                    <Rectangle HorizontalAlignment="Left" Margin="-0.208,13.238,0,-0.146" Width="10.555" Fill="#FF005167" RenderTransformOrigin="0.5,0.5">
                                <SkewTransform AngleX="-23"/>
                                <RotateTransform Angle="-12.944"/>

                    <Rectangle Fill="White" Stroke="#FF005167" RadiusX="5" RadiusY="5"/>

                    <ContentPresenter HorizontalAlignment="Center"
                                                                Content="{TemplateBinding Content}"
                                                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                                                Margin="0" TextBlock.FontFamily="Segoe UI" TextBlock.FontWeight="Bold" TextBlock.Foreground="#FFB8D30B">

You can see we added a ControlTemplate which has a TargetType for our Pushpin objects. This we will bind to our Pushpins later. I’ve made the Pushpins look a bit like our TellUs logo and for this I needed a rectangle with rounded corners. Directly behind it I placed another rectangle that I skewed and rotated a bit to make it look a bit like a balloon. Then I added a ContentPresenter that should show the numerical value and I’ve also added some font settings to it. Nothing spectacular, but it’s nice if you know how to do it! 😉

Now we move back to our code again and we need to do three things.

  1. Locate the control template for our Pushpin
  2. Bind it to our Pushpin
  3. Set the PositionOrigin of our PushPin.

This setting of the PositionOrigin is important to us, because our customized Pushpin should be placed to the top and to the right of where we click. I’ve included some code that we already had for additional clarity of where to put the code.

ControlTemplate template = (ControlTemplate)this.FindResource("CutomPushpinTemplate");

Pushpin pin = new Pushpin();
pin.Template = template;
pin.PositionOrigin = PositionOrigin.BottomLeft;
pin.Location = pinLocation;
pin.Content = counter += 10;
pin.MouseDown += new MouseButtonEventHandler(pin_MouseDown);

If we run our application again it should look like this after adding a few Pushpins and rotating them.

map2 map3

Adding custom styling based on content of the Pushpin

Now one really interesting thing to do is setting the size of the content/text of the Pushpin based on the number. If we start adding a lot of Pushpins this way and we start adding pins that have a numerical value of higher than 999, the font is too big so the value will fall outside of the boundaries of the pin.

For this we need a ValueConverter first. Based on the value that is inside the content of the Pushpin, we’ll return the FontSize. This FontSize then needs to be set to the style of the ContentPresenter. First let’s have a look at the ValueConverter. Add this directly beneath your MainWindow class, so that (in this tutorial) we don’t have the additional hassle of namespace problems.

public class PushPinContentConverter : IValueConverter
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        int pushPinContentValue;
        if (int.TryParse(value.ToString(), out pushPinContentValue))
            if (pushPinContentValue >= 1000)
                return 12;
                return 15;

        // If convert fails!
        return 14;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        throw new NotImplementedException();

As you can see we try to extract the content from the Pushpin and if the value is higher than (or equally to) a 1000, we’ll lower the FontSize. Now we need to add this converter to the static resources of the Window in the xaml. First add a namespace reference (as seen in line 5) and then add it as a resource itself (as seen in line 9).

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Width="700" Height="500">

    <local:PushPinContentConverter x:Key="MyContentConverter" />

Finally we need to add the styling to the ContentPresenter. The only thing changed is the element <ContentPresenter.Style> and the elements within it.

<ContentPresenter HorizontalAlignment="Center"
                                            Content="{TemplateBinding Content}"
                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                            Margin="0" TextBlock.FontFamily="Segoe UI" TextBlock.FontWeight="Bold" TextBlock.Foreground="#FFB8D30B">
            <Setter Property="TextBlock.FontSize" Value="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource MyContentConverter}}" />

I advise you to no start the counter variable at 0, but at 970, for your own testing pleasure. It’d be a whole lot of clicks if you started at 0 to see the results at Pushpin 1000 and higher.


All in all it is not so difficult to add a custom Pushpin, rotate it according to the map rotation and have the content sized based on its value. But information is fragmented and (for example) setting a ControlTemplate is mostly only shown in code if you start searching for it. I hope it’s beneficial for you to see it all in one place.

Download the entire solution here.

If this was usable, leave a comment and tell me your problems and/or results. Thanks!

You may also like...

4 Responses

  1. Bruce says:

    Hey this was a great tutorial and much more valuable than anything that I have found to date. However, I am looking for a solution to a couple of different scenarios. The first, rather than defining a rectangle, I would like to define a custom image for the pushpin. The second, I would like to use what you have but modify the size of the rectangle based on the content (e.g. pushpin.content = “one” will require less space than pushpin.content = “five hundred and twenty”). Not really using those values but you get what I mean.

    Any pointers would be much appreciated!

  2. Dennis van der Stelt says:

    How to do an image can be found on many places, for example the answer on this question on Stackoverflow

    The size of the pin can also be adjusted by building another IValueConverter. Remember not to animate/adjust the size, but use transform to make it bigger. That’s performance wise much better.

  3. Faro says:

    Hi Dennis,

    do you know any way that the map can recognize an object when I put a tagged object on it?

  4. DanDes says:

    Thanks – I got this to work as a <UserControl.Resources> in my WPF Map UserControl. This made it possible to then reference the template in a Winforms application – which was my goal.
    The changes were, instead of <Windows.Resources>…</Windows.Resources> I put the template inside of a <UserControl.Resources>…</UserControl.Resources> block.
    Then in the Winforms application I used the resource template as follows…
    Dim custPinTemplate as System.Windows.Controls.ControlTemplate = [MyWPFUserControl].FindResource(“CustomPushpinTemplate”)
    I was then able to assign the template to the pushpin with
    MyPushpin.Tempate = custPinTemplate

    Now – if anyone has a hint on how I might use a small pushpin png file instead of all the drawing instructions, that would be helpful…

Leave a Reply

Your email address will not be published. Required fields are marked *