Using pushpins in a DataTemplateSelector

2019-08-13 00:31发布

I have built a DataTemplateSelector which I use for showing a different pushpin on the map. I now have the following DataTemplate wich works in the way I want, except that it are not pushpins but textblocks which are shown on the map.

<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>

I would expect that it would work in the following format too:

<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>

But with this template it's only showing 1 black pushpin on the map. Am I doing something wrong with the Bindings? I'am not seeing why this is not working in the expected way.

As requested by @localjoost the code for the datatemplateselector:

The abstract class:

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);
    }
}

And the implementation of the abstract class:

     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;
        }
    }
}

And how I use the Datatemplate (which is defined in my application resources section) in my map control:

<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>

As far is I can see there's not rocket science inside the pushpinselectortemplate.

2条回答
Viruses.
2楼-- · 2019-08-13 00:37

I usually do this using multiple layers. This is a piece of code that comes from an actual working program. Maybe adding a Layer helps you out?

<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>
查看更多
甜甜的少女心
3楼-- · 2019-08-13 00:50

Haven't found a different approach than the first one described in the question. For now I will work with it in that way.

查看更多
登录 后发表回答