我正在尝试自定义地图图钉的模板。不是windows 8 mobile,这只是一个WPF地图控件。我无法让XAML识别TargetType。我必须指定TargetType,以便我可以修改在通用Control中找不到的某些元素。我尝试了几种变体。代码存在于与WPF UserControl分开的XAML文件中(在MergedDictionaries中引用)。代码如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<ControlTemplate x:Key="PushPinTemplate" TargetType="m:PushPin" >
<Grid x:Name="ContentGrid">
<StackPanel Orientation="Vertical">
<Grid Background="{TemplateBinding Background}"
HorizontalAlignment="Left"
MinHeight="31"
MinWidth="29">
<ContentPresenter HorizontalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Margin="4"/>
</Grid>
<Polygon Fill="{TemplateBinding Background}"
Points="0,0 29,0 0,29"
Width="29"
Height="29"
HorizontalAlignment="Left"/>
</StackPanel>
</Grid>
</ControlTemplate>
</ResourceDictionary>
在C#中设置PushPin.Template
pushpin.Template = (ControlTemplate)(Resources["PushPinTemplate"]);
答案 0 :(得分:3)
类型名称未正确写入。它是Pushpin
,而不是PushPin
:
<ControlTemplate ... TargetType="m:Pushpin">
答案 1 :(得分:1)
您错过了{x:Type }
声明
<ControlTemplate x:Key="PushPinTemplate" TargetType="{x:Type m:PushPin}" >
这意味着您要向TargetType提供字符串而不是Type
x:Type标记扩展为采用Type类型的属性提供from-string转换行为。输入是XAML类型。
http://msdn.microsoft.com/en-us/library/ms753322%28v=vs.110%29.aspx