2016-08-19 16 views
0

私はContentPresenterを持っており、ContentTemplate属性にDataTemplateを割り当てました。今私は、以下に示すようなDataTemplateの子であるMapControlMapIconを追加したい: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の概念を使用してのMapControlを得ることができますか?

答えて

1

VisualContentのC#の概念を使用して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; 
} 
:あなたは MapControlには、次のコードを使用して取得する VisualTreeHelperを使用することができます

関連する問題