2016-08-19 56 views
0

我有一个ContentPresenter,并且我已将DataTemplate分配给其ContentTemplate属性。现在我想将在MapControl是DataTemplate中的孩子MapIcon添加如下图所示:如何从UWP C#中的ContentPresenter DataTemplate获取MapControl?

<DataTemplate x:Key="EWDetailsContentTemplate" x:DataType="viewModels:Task"> 

     <Grid x:Name="ContentPanel" 
        Background="White" 
        Margin="0,5,0,0"> 

      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 

      <Maps:MapControl x:Name="LocationMapControl" 
          MapServiceToken="key" 
          Grid.Row="0" 
          Height="250"/> 
     //more controls 
     </Grid> 

我怎样才能使用地图控件的C#的VisualTree概念?

回答

1

如何使用C#的VisualTree概念获取MapControl?

您可以使用VisualTreeHelper得到MapControl使用如下代码:

//This function will get all the children Control of one Controls' Container 
public List<Control> AllChildren(DependencyObject parent) 
{ 
    var _List = new List<Control>(); 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
    { 
     var _Child = VisualTreeHelper.GetChild(parent, i); 
     if (_Child is Control) 
      _List.Add(_Child as Control); 
     _List.AddRange(AllChildren(_Child)); 
    } 
    return _List; 
} 

private MapControl GetMapControl() 
{ 
    var controls = AllChildren(myContentPresenter);//"myContentPresenter" is your ContentPresenter's name. 
    var mapControl = (MapControl)controls[0]; 
    return mapControl; 
} 
相关问题