WPF Bing地图中的信息框

doc*_*c92 5 wpf xaml bing-maps pushpin

我最近开始在WPF中做一些事情,我想出了将地图集成到我的应用程序中的想法.我尝试了谷歌地图的一些东西,但功能不是很好,所以过了一段时间我放弃了在WPF上的谷歌地图.

过了一会儿,我遇到了Bing Maps.这看起来比谷歌地图更有希望与WPF一起使用.我开始玩Bing的地图,功能很棒!

然而,当我试图在地图上放一个图钉时,我不能立即清楚如何在图钉悬停在图钉上时添加一个信息框.我已经找到了一些如何操作的示例,但它需要与xaml链接的过程代码.我实际上是在寻找一种不使用过程代码的方法.

是否可以仅使用xaml为图钉添加信息框?或者有没有人有一个很好的替代方法如何这样做?

虽然有一个工具提示属性,但我实际上并没有找到它.我实际上是在寻找谷歌地图的图钉式(如果有的话).

小智 8

假设我理解你想要的东西,我相信简短的答案是:抱歉,但是不可能只用XAML将图钉添加到Google地图样式的信息框中.但是,如果可以,我会尽力帮助.

免责声明:我一直在玩Silverlight的Bing Maps控件,所以希望这也适用于WPF版本的控件.

我想你不想使用内置的工具提示,因为你希望它看起来不同(即不只是一个带文字的黄色框),或者因为你希望它在用户移开鼠标时不会消失.

如果你只是想让它看起来与众不同,我可以提供以下内容.当我为我的Pushpins指定模板时,我继续使用重新模板化的工具提示,并允许用户单击图钉以获取更多信息.

使用自定义工具提示的Bing地图

这是ToolTip模板,定义为StaticResource,当然可以包含您想要的任何内容:

<Style x:Key="MyToolTipStyle" TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border CornerRadius="5" BorderBrush="Black" BorderThickness="2" Background="#5c87b2">
                    <ContentPresenter Margin="5">
                        <ContentPresenter.Content>
                            <StackPanel Margin="5" MaxWidth="400">
                                <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="16" Foreground="White" TextWrapping="Wrap"/>
                                <TextBlock Text="{Binding Description}" Foreground="White" TextWrapping="Wrap"/>
                            </StackPanel>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这是我用它的地方:

<maps:Map>
    <maps:MapItemsControl ItemsSource="{Binding SearchResultsManager.Items}">
        <maps:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <maps:Pushpin Location="{Binding Location}" Cursor="Hand" MouseLeftButtonUp="Pushpin_MouseLeftButtonUp">
                    <ToolTipService.ToolTip>
                        <ToolTip Style="{StaticResource MyToolTipStyle}" />
                    </ToolTipService.ToolTip>
                </maps:Pushpin>
            </DataTemplate>
        </maps:MapItemsControl.ItemTemplate>
    </maps:MapItemsControl>
</maps:Map>
Run Code Online (Sandbox Code Playgroud)

然后,当用户点击图钉将其带到详细信息区域时,我会处理.

private void Pushpin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    // Get a reference to the object that was clicked.
    var clickedSearchResult = (sender as FrameworkElement).DataContext as SearchResultViewModel;

    // Do something with it.
}
Run Code Online (Sandbox Code Playgroud)

但是,我想你想让ToolTip不再消失,这样用户就可以点击里面的控件了.不幸的是,我不确定有一个简单的方法可以做到这一点.您可能必须定义自己的自定义控件,当然这需要一些C#/ VB代码.

也许这个新控件可能来自Pushpin,它可以显示鼠标悬停和/或点击的信息框内容.您可以使用VisualStateManager将大部分代码保留在XAML中.C#/ VB只需要为内容提供依赖属性,并在一些正确的时间覆盖视觉状态之间的一些覆盖.

我希望至少有一点帮助!