I'm working on an application that uses the WPF Bing Maps library. The DataContext
of the map control I'm working on has a list of GPS coordinates (Location
objects), each of which should be represented by a Pushpin
element on the map. I was hoping to add these pushpins to the map through XAML, using an ItemsControl
(simplified for brevity):
<m:Map>
<ItemsControl ItemsSource="{Binding GPSCoordinates}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<m:Pushpin Location="{Binding}">
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</m:Map>
This doesn't work, however, because the Pushpin
elements don't get added directly to the Map
element; rather, they are encapsulated by the ItemsControl
's ItemsPanel
and the ItemsControl
element itself. Is there a way to generate a list of items and have the items defined in the DataTemplate
added directly to the ItemsControl
's parent?
In other words, I'd like the Visual Tree to look like this:
<m:Map>
<m:Pushpin />
<m:Pushpin />
...
</m:Map>
instead of this:
<m:Map>
<ItemsControl>
<StackPanel>
<m:Pushpin />
<m:Pushpin />
...
</StackPanel>
</ItemsControl>
</m:Map>
Although it does not add Pushpins as direct children of the Map control, MapItemsControl
is what you are looking for:
<m:Map ...>
<m:MapItemsControl ItemsSource="{Binding GPSCoordinates}">
<m:MapItemsControl.ItemTemplate>
<DataTemplate>
<m:Pushpin Location="{Binding}"/>
</DataTemplate>
</m:MapItemsControl.ItemTemplate>
</m:MapItemsControl>
</m:Map>