2016-10-11 37 views
1

多的DataTemplates我有一個MapControl在UWP:與UWP MapItemsControl

<maps:MapControl x:Name="BikeMap" ZoomLevel="17" Center="{Binding CenterPoint, Mode=TwoWay}"> 
    <maps:MapItemsControl x:Name="MapItems" ItemsSource="{Binding BikePoints}" 
         ItemTemplate="{StaticResource BikePointTemplate}"/> 
</maps:MapControl> 

,並使用XAML數據模板添加我MapElements,我的ItemsSource是簡單對象的列表。

但是,UWP似乎並沒有提供一種方式來指定一個DataTemplateDataTypeMapItemsControl沒有用於設置DataTemplateSelector的屬性。

有誰知道我可以如何在MapItemsControl中使用多個數據模板,並根據ItemsSource中的對象類型選擇相關的數據模板?

+0

很難相信UWP MapItemsControl不是從ItemsControl派生的。你也許可以選擇一個不同的地圖庫,其中MapItemsControl實際上是一個帶有工作ItemTemplateSelector的ItemsControl,就像[this one](https://xamlmapcontrol.codeplex.com/)。 – Clemens

+0

你好@ JayZuo-MSFT我還沒有嘗試過,因爲一直很忙。我很快就會接受!謝謝 –

回答

5

MapItemsControl Class沒有設置DataTemplateSelector的屬性。要達到您想要的效果,我們可以利用ContentControl將其設置爲DataTemplate中的模板內容,然後使用ContentControl.ContentTemplateSelector屬性設置DataTemplateSelector

下面是一個簡單的示例:

XAML:

<Page x:Class="UWPApp.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="using:UWPApp" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d"> 
    <Page.Resources> 
     <DataTemplate x:Key="GreenDataTemplate"> 
      <StackPanel Background="Green"> 
       <TextBlock Margin="5" 
          Maps:MapControl.Location="{Binding Location}" 
          Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" 
          FontSize="20" 
          Text="{Binding Name}" /> 
      </StackPanel> 
     </DataTemplate> 
     <DataTemplate x:Key="RedDataTemplate"> 
      <StackPanel Background="Red"> 
       <TextBlock Margin="5" 
          Maps:MapControl.Location="{Binding Location}" 
          Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" 
          FontSize="20" 
          Text="{Binding Name}" /> 
      </StackPanel> 
     </DataTemplate> 

     <local:MyTemplateSelector x:Key="MyTemplateSelector" GreenTemplate="{StaticResource GreenDataTemplate}" RedTemplate="{StaticResource RedDataTemplate}" /> 
    </Page.Resources> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Maps:MapControl x:Name="MyMap" MapServiceToken="MapServiceToken"> 
      <Maps:MapItemsControl x:Name="MyMapItemsControl" ItemsSource="{Binding}"> 
       <Maps:MapItemsControl.ItemTemplate> 
        <DataTemplate> 
         <ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource MyTemplateSelector}" /> 
        </DataTemplate> 
       </Maps:MapItemsControl.ItemTemplate> 
      </Maps:MapItemsControl> 
     </Maps:MapControl> 
    </Grid> 
</Page> 

代碼隱藏:

public class MyTemplateSelector : DataTemplateSelector 
{ 
    public DataTemplate GreenTemplate { get; set; } 
    public DataTemplate RedTemplate { get; set; } 

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) 
    { 
     if (item != null) 
     { 
      if (item is GreenPOI) 
      { 
       return GreenTemplate; 
      } 

      return RedTemplate; 
     } 

     return null; 
    } 
} 

public class POI 
{ 
    public string Name { get; set; } 

    public Geopoint Location { get; set; } 
} 

public class GreenPOI : POI { } 

public class RedPOI : POI { } 

這只是舉例。在示例中,我使用了具有不同背景的兩個數據模板,並且我創建了一個自定義DataTemplateSelector,可以根據對象類型選擇DataTemplate。如果你有幾種對象類型,你也可以參考這個答案:How to associate view with viewmodel or multiple DataTemplates for ViewModel?