Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I needed to add a Bing Map implementation to a winforms application written in VB.

What I have tried:

It wasn't that hard using the WPF Interoperability control. Once I created the WPF map control I was able to import it into my project and it works fine.
My problem is that there is only one default pushpin style. I would have expected a panoply of them... Silly me.
My new requirement is to have a second pushpin style, but all of the examples I've located show how to do it in XAML in WPF, or on a web page.
Here is the current code for inserting a pushpin on the WPF control in a winforms application

Private Sub AddPin(PinName as String, Lat as Double, Lon as Double)
  Dim Pin as New Microsoft.Maps.MapControl.WPF.Pushpin
  Pin.Location = New Microsoft.Maps.MapControl.WPF.Location(Lat, Lon)
  Pin.ToolTip = PinName
  Pin.Name = PinName
  MyBingMapUC.Map.Center = Pin.Location
  MyBingMapUC.Map.ZoomLevel = STATE_LEVEL '5
  AddHandler Pin.MouseDown, AddressOf PushpinClick
  BingMapUC.Map.Children.Add(Pin)
End Sub

Private Sub PushpinClick(sender As Object, e As Windows.RoutedEventArgs)
  e.handled = True
  MsgBox("pin " & DirectCast(sender, Microsoft.Maps.MapControl.WPF.Pushpin).Name & " was clicked!")
End sub


The above adds the default pushpin. I cannot find any way to change the pushpin using the properties or methods within the pushpin class. There is a pushpin template property. If that is to be some sort of XAML template there are no clues as to how to create it, or incorporate it into my WinForms application.

I would really appreciate it if someone could point me in the right direction...
Posted
Updated 10-Mar-17 10:02am

 
Share this answer
 
Thanks Graeme - I did run across that one - but decided to run through each step of it slowly and deliberately. In doing so I found out how to do what I needed - so, although this was still more about working entirely within WPF, I was able to "interpolate" what I needed. So for anyone else wanting to enable custom pushpins on a WPF control in a WinForms application - here's how it's done.
In your WPF usercontrol you will need to add a section called <usercontrol.resources> as follows
<UserControl.Resources>
   <ControlTemplate x:Key="yourtemplatenamehere" TargetType="m:Pushpin">
   .... the template constructors here
   </ControlTemplate>
</UserControl.Resources>

Put this second just above the <grid> section.

Now the example had a fairly ugly pushpin defined in the ControlTemplate, so I improved on it (with a lot of experimenting). So my entire ControlTemplate looks like this
<UserControl.Resources >
  <ControlTemplate x:Key="MyNewPushpin" TargetType="m:Pushpin">
    <Grid x:Name="ContentGrid" HorizontalAlignment="Center" VerticalAlignment="Center">
     <StackPanel>
       <Grid Margin="0" Width="24" Height="23">
        <Rectangle HorizontalAlignment="Left"  Margin="0,3.238,0,-2.146" Width="23" Height="18" Fill="SteelBlue" Stroke="Black"  RenderTransformOrigin="0.5,0.62">
         <Rectangle.RenderTransform>
          <TransformGroup>
            <ScaleTransform/>
            <SkewTransform AngleY="0" AngleX="-23"/>
            <RotateTransform Angle="123"/>
            <TranslateTransform/>
           </TransformGroup>
          </Rectangle.RenderTransform>
         </Rectangle>
         <Rectangle Fill="{TemplateBinding Background}" Stroke="black" RadiusX="5" RadiusY="5"/>
           <ContentPresenter HorizontalAlignment="Center"                                                                VerticalAlignment="Center"                                                                Content="{TemplateBinding Content}"                                                                ContentTemplate="{TemplateBinding ContentTemplate}"                                                                Margin="0" TextBlock.FontFamily="Segoe UI" TextBlock.FontWeight="Bold" TextBlock.Foreground="Black" >
           </ContentPresenter>
     </Grid>
    </StackPanel>
   </Grid>
 </ControlTemplate>
</UserControl.Resources>

So now the trick is to figure out how to get access to this template in winforms...
Well, the UserControl is hosted in my Winforms application through the use of the WPF Interoperability control. It, like your textboxes and labels and panels is one of the controls available in you toolbox once you have added a reference to WindowsFOrmsIntegration and the Microsoft.Maps.MapControl.WPF. To get those you need to download and install the WPF Map SDK that you get from here...
https://msdn.microsoft.com/en-us/library/hh750210.aspx?f=255&MSPPError=-2147217396
When you add the WPF Interoperability control you get a chance to add the WPF control you've built.
You can now access the template within that control as follows

Dim NewPushpin As System.Windows.Controls.ControlTemplate = BingMapUC.FindResource("MyNewPushpin")


Then instantiate and present your pushpin(s) in code

Dim Pin As New Pushpin
Pin.Location = New Location(Latitude, Longitude)
Pin.Content = "a pin"
Pin.Name = "Pin_" & Counter
Pin.Template = NewPushpin
Pin.Background = Windows.Media.Brushes.YellowGreen
MyBingMapUC.Map.Center = Pin.Location
MyBingMapUC.Map.ZoomLevel = STATE_VIEW
MyBingMapUC.Map.Children.Add(Pin)

In my template I exposed the Pushpin background so it can be changed as necessary. This gave me some flexibility I needed.

If you don't know how to put together a WPF usercontrol for use on a winform there's a decent tutorial here: https://blogs.msdn.microsoft.com/rbrundritt/2013/11/08/using-bing-maps-in-winforms/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900