如何从 UWP C# 中的 ContentPresenter DataTemplate 获取 MapControl?
How to get MapControl from ContentPresenter DataTemplate in UWP C#?
我有一个 ContentPresenter,我已将 DataTemplate
分配给它的 ContentTemplate
属性。现在我想在 MapControl
中添加一个 MapIcon
,它是 DataTemplate 的子项,如下所示:
<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?
How can I get MapControl using VisualTree concept of C#?
您可以使用 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;
}
我有一个 ContentPresenter,我已将 DataTemplate
分配给它的 ContentTemplate
属性。现在我想在 MapControl
中添加一个 MapIcon
,它是 DataTemplate 的子项,如下所示:
<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?
How can I get MapControl using VisualTree concept of C#?
您可以使用 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;
}