我有以下 XAML 代码:
<maps:Map Name="PlaygroundMap" Credentials="YOUR_BING_MAPS_KEY">
   <maps:MapItemsControl  
           x:Name="PlaygroundMapItems"
           my:MapExt.ClusteredItemsSource="{Binding Locations}" 
           ItemTemplate="{StaticResource PinTemplate}"/>
 </maps:Map>  
ClusteredItemsSource也是我自己的附加属性,带有以下代码(仅显示重要部分):
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;
        }
所以我的要求是在 OnItemsReceived 方法中有 MAP 参考。我认为这很容易,因为如果再次检查 XAML,您将看到 mapItemsControl 是 Map 的子级(或 map 是 mapItemsControl 的父级)。
但不知何故,变量 map 和 map2 始终为 NULL。这是正常行为还是?
此外,如果这种“父”方法不起作用,您能否建议我在 MapItemsControl 中获取地图参考的其他选项(准确地说,在我的自定义附加属性中获取地图参考)。
顺便说一句,我正在为 WinRT 使用 Bing Maps SDK。