【问题标题】: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<Canvas> 并将它们相互绘制?
【问题讨论】:
标签:
c#
.net
wpf
google-maps
gis
【解决方案1】:
您似乎有些困惑。 ObservableCollection 是数据项的集合。 Canvas 是一个 UI 控件。因此,您永远不应该有一个包含 UI 元素的数据项集合。
相反,您可以拥有一个 Marker 数据类,其中包含标记位置的 X 和 Y 属性,还可以使用 Name 和 ImageSource 属性来定义它在 UI 中的外观。然后你可以有一个名为Markers 的Marker 类型的ObservableCollection,你可以绑定到ItemsControl 的ItemsSource 属性。最后,您可以将Canvas 设置为ItemsControl 的ItemsPanel:
<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 元素.