I have following XAML code :
<maps:Map Name="PlaygroundMap" Credentials="YOUR_BING_MAPS_KEY">
<maps:MapItemsControl
x:Name="PlaygroundMapItems"
my:MapExt.ClusteredItemsSource="{Binding Locations}"
ItemTemplate="{StaticResource PinTemplate}"/>
</maps:Map>
Also ClusteredItemsSource is my own attached property with the following code (only important part is shown) :
private static void OnItemsReceived(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mapItemsControl = d as MapItemsControl;
if(mapItemsControl == null) return;
var map = VisualTreeHelper.GetParent(mapItemsControl); // this is always NULL
var map2 = mapItemsControl.Parent; // this is always NULL
var items = e.NewValue as IEnumerable<Location>;
mapItemsControl.ItemsSource = items;
}
So my requirement is to have MAP reference in OnItemsReceived method. I thought that this will be easy because if you check XAML again you will see that mapItemsControl is child of Map (or map is parent of mapItemsControl).
But somehow variable map & map2 are always NULL. Is this normal behaviour or?
Also, if this “parent” approach will not work, can you suggest me what are my other options to get Map reference in MapItemsControl (to be exact, to get map reference in my custom attached property).
Btw, I’m using Bing Maps SDK for WinRT.
VisualTreeHelper won’t help you if your controls are not part of the visual tree – either because they were not added to one yet (Loaded event wasn’t raised yet) or maybe they don’t use the standard parent/child control relationship triggers (by one being logically a child of a Panel, ContentControl etc.)
The best approach here really is to forget about the visual tree and use bindings and view models instead.