将DataTemplate和ControlTemplate设置为WPF中的Map Pushpins时出现问题

时间:2013-06-15 13:38:59

标签: wpf wpf-controls bing-maps pushpin

我无法将DataTemplateControlTemplate设置到我的bing地图图钉上。当我尝试通过添加ControlTemplate来自定义我的图钉时,我正在使用数据绑定。

我的代码:

<UserControl x:Class="BingMap.MapUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" 
         xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
         xmlns:viewPushpins="clr-namespace:Program.Map_Control.ViewPushpins">

<UserControl.DataContext>
    <viewPushpins:MapViewPushpins/>
</UserControl.DataContext>

<UserControl.Resources>
    <DataTemplate x:Key="PushpinDataTemplateP">
        <m:Pushpin Location = "{Binding MapLocationP}" ToolTip="{Binding MapTooltipTextP}"/>
    </DataTemplate>
    <ControlTemplate x:Key="PushpinControlTemplateP">
        <Grid>
            <Ellipse Fill="Green" Width="15" Height="15" />
        </Grid>
    </ControlTemplate>
</UserControl.Resources>

<Grid>
    <m:Map  Name="myMap"
            CredentialsProvider="..."
            ZoomLevel="1"
            Center="30,-100"
            Mode="AerialWithLabels"
            MouseLeftButtonUp="Map_Left_Click_Up">
        <m:MapItemsControl
            Template="{StaticResource PushpinControlTemplateP}"
            ItemTemplate="{StaticResource PushpinDataTemplateP}" MouseLeftButtonUp="Map_Left_Click_Up"
            ItemsSource="{Binding  MapLocationsP}"/>
    </m:Map>
</Grid>

</UserControl>

如果我删除该行,则此代码有效:

Template="{StaticResource PushpinControlTemplateP}"

但后来没有得到我自定义的图钉。

关于如何解决此问题的任何想法?

2 个答案:

答案 0 :(得分:1)

通过设置MapItemsControl.Template,您为MapItemsControl本身指定了一个模板,而不是它生成的项目。您可以通过ItemsControl

间接设置ItemContainerStyle生成的各个项目的模板
<Style x:Key="PushPinStyle">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <!-- your template goes here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<m:MapItemsControl ItemContainerStyle="{StaticResource }" ...>

答案 1 :(得分:1)

我知道我错了。正如Kent已经提到的那样,

Template="{StaticResource PushpinControlTemplateP}"

不应该在MapItemsControl中。

我通过将这行代码移到Resources来解决了我的问题所以它们看起来像这样:

<UserControl.Resources>
<ControlTemplate x:Key="PushpinControlTemplateP">
    <Grid>
        <Ellipse Fill="Green" Width="15" Height="15" />
    </Grid>
</ControlTemplate>
<DataTemplate x:Key="PushpinDataTemplateP">
    <m:Pushpin Location = "{Binding MapLocationP}" ToolTip="{Binding MapTooltipTextP}" Template="{StaticResource PushpinControlTemplateP}"/>
</DataTemplate>
</UserControl.Resources>