XAML 为地图图钉创建控件模板



我正在尝试自定义地图图钉的模板。不是Windows 8移动版,这只是一个WPF地图控件。我无法让 XAML 识别目标类型。我必须指定 TargetType,以便我可以修改在泛型控件中找不到的某些元素。我已经尝试了几种变体。代码存在于独立于 WPF 用户控件的 XAML 文件中(在合并词典中引用)。代码如下:

<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"]);

类型名称未正确写入。它是Pushpin,而不是PushPin

<ControlTemplate ... TargetType="m:Pushpin">

您错过了{x:Type }声明

<ControlTemplate x:Key="PushPinTemplate" TargetType="{x:Type m:PushPin}" >

这意味着您向目标类型提供字符串而不是类型

x:Type 标记扩展为采用 Type 类型的属性提供从字符串转换行为。输入是 XAML 类型。

http://msdn.microsoft.com/en-us/library/ms753322%28v=vs.110%29.aspx

最新更新