【问题标题】:Drawing multiple layers over wpf map control在 wpf 地图控件上绘制多个图层
【发布时间】:2013-12-04 09:22:42
【问题描述】:

我有带有 wpf 地图控件的网格和用于在地图上绘制标记的画布。

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <UserControl Grid.Row="0" Content="{Binding CurrentMap}" />
    <UserControl Grid.Row="0" Content="{Binding DrawningCanvas}" />
</Grid>

现在我需要在地图上使用超过 1 层。

我应该使用什么 wpf 控件来绑定到 DataContext 中的 ObservableCollection&lt;Canvas&gt; 并将它们相互绘制?

【问题讨论】:

    标签: c# .net wpf google-maps gis


    【解决方案1】:

    您似乎有些困惑。 ObservableCollection 是数据项的集合。 Canvas 是一个 UI 控件。因此,您永远不应该有一个包含 UI 元素的数据项集合。

    相反,您可以拥有一个 Marker 数据类,其中包含标记位置的 XY 属性,还可以使用 NameImageSource 属性来定义它在 UI 中的外观。然后你可以有一个名为MarkersMarker 类型的ObservableCollection,你可以绑定到ItemsControlItemsSource 属性。最后,您可以将Canvas 设置为ItemsControlItemsPanel

    <ItemsControl ItemsSource="{Binding Markers}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type YourXmlNamespacePrefix:Marker}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImageSource}" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    我真的不知道你为什么想要多个图层,因为你可以在同一图层上叠加多个项目,但如果你真的需要多个图层,那么你可以添加更多上面显示的 ItemsControl 元素.

    【讨论】:

    • 非常感谢,我认为这个解决方案解决了我的问题。
    猜你喜欢
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2018-02-21
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多