XAML Create ControlTemplate for Map PushPin
我正在尝试自定义地图图钉的模板。不是 windows 8 移动版,这只是一个 WPF 地图控件。我无法让 XAML 识别 TargetType。我必须指定 TargetType,以便我可以修改在通用控件中找不到的某些元素。我尝试了几种变体。该代码存在于与 WPF UserControl 分开的 XAML 文件中(在 MergedDictionaries 中被引用)。下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <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
1 | pushpin.Template = (ControlTemplate)(Resources["PushPinTemplate"]); |
类型名称写不正确。它是
1 | <ControlTemplate ... TargetType="m:Pushpin"> |
你错过了
1 | <ControlTemplate x:Key="PushPinTemplate" TargetType="{x:Type m:PushPin}"> |
这意味着您正在向 TargetType 提供字符串而不是 Type
The x:Type markup extension supplies a from-string conversion behavior for properties that take the type Type. The input is a XAML type.
http://msdn.microsoft.com/en-us/library/ms753322(v=vs.110).aspx