【问题标题】:Multiple DataTemplates with UWP MapItemsControl使用 UWP MapItemsControl 的多个 DataTemplates
【发布时间】:2016-10-11 00:53:26
【问题描述】:

我在 UWP 中有一个MapControl

<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 似乎没有提供指定DataTemplateDataType 的方法,而MapItemsControl 没有用于设置DataTemplateSelector 的属性。

有谁知道我如何在 MapItemsControl 中使用多个数据模板并根据 ItemsSource 中的对象类型选择相关数据模板?

【问题讨论】:

  • 很难相信 UWP MapItemsControl 不是从 ItemsControl 派生的。您可能会选择一个不同的地图库,其中 MapItemsControl 实际上是一个 ItemsControl 与一个工作 ItemTemplateSelector,如 this one
  • 你好@JayZuo-MSFT 我还没有尝试过,因为一直很忙。我会尽快解决的!谢谢

标签: xaml uwp windows-10-universal uwp-xaml uwp-maps


【解决方案1】:

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?

【讨论】:

  • 这是一个很好的解决方案,但是地图控件在初始绑定和渲染后不会添加更多项目......我似乎无法弄清楚为什么。仅绘制初始项目。
  • 啊哈,[new ObservableCollection] 无法更新,但 [new ObservableCollection] 有效...不知道为什么会这样。
猜你喜欢
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 2012-03-13
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多