在DataTemplateSelector使用图钉(Using pushpins in a Data

2019-10-16 15:33发布

我建立了我使用的地图上显示不同的图钉一个DataTemplateSelector。 我现在有以下的DataTemplate至极在我希望的方式工作,但它不是图钉,但其显示在地图上的TextBlocks。

<DataTemplate x:Key="pushpinSelector">
    <my:Pushpin Location="{Binding Location}" Tap="Pushpin_Tap">
        <my:Pushpin.Template>
            <ControlTemplate>
                <local:PushpinTemplateSelector Content="{Binding}">
                    <local:PushpinTemplateSelector.ClusterTemplate>
                        <DataTemplate>
                            <TextBlock Text="Cluster" Width="100" Foreground="YellowGreen"></TextBlock>
                        </DataTemplate>
                    </local:PushpinTemplateSelector.ClusterTemplate>
                    <local:PushpinTemplateSelector.PushpinTemplate>
                        <DataTemplate>
                            <TextBlock Text="Pushpin" Width="100" Foreground="Blue"></TextBlock>
                        </DataTemplate>
                    </local:PushpinTemplateSelector.PushpinTemplate>
                </local:PushpinTemplateSelector>
            </ControlTemplate>
        </my:Pushpin.Template>
    </my:Pushpin>
</DataTemplate>

我希望它会在以下格式工作过:

<DataTemplate x:Key="pushpinSelector">
    <local:PushpinTemplateSelector Content="{Binding}">
        <local:PushpinTemplateSelector.ClusterTemplate>
            <DataTemplate>
                <my:Pushpin Location="{Binding Location}" Content="{Binding Count}" Foreground="YellowGreen"></my:Pushpin>
            </DataTemplate>
        </local:PushpinTemplateSelector.ClusterTemplate>
        <local:PushpinTemplateSelector.PushpinTemplate>
            <DataTemplate>
                <my:Pushpin Location="{Binding Location}" Foreground="Blue"></my:Pushpin>
            </DataTemplate>
        </local:PushpinTemplateSelector.PushpinTemplate>
    </local:PushpinTemplateSelector>
</DataTemplate>

但与此模板它只是显示在地图上1个黑色图钉。 我做得不对的绑定? 我'没有看到这是为什么不以预期的方式工作。

按照要求通过@localjoost的datatemplateselector的代码:

抽象类:

public abstract class DataTemplateSelector : ContentControl
{
    public virtual DataTemplate SelectTemplate(
        object item, DependencyObject container)
    {
        return null;
    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        ContentTemplate = SelectTemplate(newContent, this);
    }
}

和抽象类的实现:

     public class PushpinTemplateSelector : DataTemplateSelector
{
    public DataTemplate ClusterTemplate
    {
        get;
        set;
    }

    public DataTemplate PushpinTemplate
    {
        get;
        set;
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        PushpinModel model = item as PushpinModel;

        if (model.CurrentObject == null)
        {
            return ClusterTemplate;
        }
        else
        {
            return PushpinTemplate;
        }
    }
}

我该如何在我的地图控件使用DataTemplate中(这是在我的应用程序资源部分中定义):

<my:Map Height="624" HorizontalAlignment="Left" Name="map1" VerticalAlignment="Top" Width="468" CredentialsProvider="XXXXX" 
        ZoomLevel="13">
    <my:MapItemsControl Name="pushPinModelsLayer" ItemsSource="{Binding PushpinModels}" ItemTemplate="{StaticResource pushpinSelector}" />
    <my:Pushpin Name="myLocation" Template="{StaticResource MyLocationTemplate}"></my:Pushpin>
</my:Map>

至于是我可以看到有没有pushpinselectortemplate里面火箭科学。

Answer 1:

我通常做到这一点使用多层。 这是一段代码,来自实际的工作计划。 也许添加图层可以帮助你?

<Microsoft_Phone_Controls_Maps:Map x:Name="map"
                   CredentialsProvider="blah">
  <Microsoft_Phone_Controls_Maps:MapLayer x:Name="MapLayer_GasStations">
    <Microsoft_Phone_Controls_Maps:MapItemsControl 
      ItemsSource="{Binding GasStations}" 
      ItemTemplate="{StaticResource GasStationViewModelTemplate}" />
  </Microsoft_Phone_Controls_Maps:MapLayer>
  <Microsoft_Phone_Controls_Maps:MapLayer x:Name="MapLayer_RoadBlocks">
    <Microsoft_Phone_Controls_Maps:MapItemsControl 
      ItemsSource="{Binding RoadBlocks}" 
      ItemTemplate="{StaticResource RoadBlockViewModelTemplate}" />
  </Microsoft_Phone_Controls_Maps:MapLayer>
</Microsoft_Phone_Controls_Maps:Map>


Answer 2:

有没有发现比在问题中所述的第一个不同的方法。 现在我会以这种方式使用它。



文章来源: Using pushpins in a DataTemplateSelector