【问题标题】:XAML Create ControlTemplate for Map PushPinXAML 为地图 PushPin 创建 ControlTemplate
【发布时间】:2023-04-07 11:10:02
【问题描述】:

我正在尝试自定义地图图钉的模板。不是 windows 8 移动版,这只是一个 WPF 地图控件。我无法让 XAML 识别 TargetType。我必须指定 TargetType,以便我可以修改在通用控件中找不到的某些元素。我尝试了几种变体。该代码存在于与 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"]);

【问题讨论】:

  • 对不起,我还没有在 WPF 中做过,但我知道当我在商店应用程序或 SL 等中这样做时,我使用样式模板作为图钉而不是 ControlTemplate,然后它可以被称为pushpin.Style = this.Resources["PushPinTemplate"] as Style; 相反,不确定这是否有帮助,所以只是添加为评论。

标签: wpf xaml bing-maps pushpin


【解决方案1】:

类型名称写不正确。是Pushpin,不是PushPin

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

【讨论】:

  • 我证实这实际上是问题所在。我在外壳之间来回走动,这就是问题如此不一致的原因。谢谢!
【解决方案2】:

您错过了{x:Type } 声明

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

这意味着您正在为 TargetType 而不是 Type 提供字符串

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

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

【讨论】:

  • 以为我以前试过这个。这行得通。谢谢!
  • TargetType="m:PushPin" 是有效的 XAML。不知道从哪个 WPF 版本开始,但它被隐式处理为类型,而不是字符串,就像你写了 {x:Type ...} 一样。这里的问题是正确的类型名称是Pushpin,而不是PushPin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多