从数据绑定映射中获取数据查看项目的上下文菜单

时间:2012-07-30 03:08:28

标签: windows-phone-7 data-binding bing-maps

我正在编写一个Windows Phone应用程序,它在地图上显示了许多位置。这些职位有电话号码,推特账号和与之相关的网站。

我想要做的是为允许用户打开网站,查看推特或拨打电话号码的项目添加上下文菜单。但是,使用MapItemsControl,获取当前项目并不容易。

我正在寻找一些方法来尽可能轻松地做到这一点。

一些代码:

<mi:MapItemsControl x:Name="mapPins" ItemsSource="{Binding Pushpins}">
    <mi:MapItemsControl.ItemTemplate>
        <DataTemplate>
            <mi:Pushpin Location="{Binding Location}">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu IsEnabled="{Binding ShowContext}" >
                        <TextBlock Opacity="{Binding Opacity, ElementName=image}" Text="Show Menu"/>
                        <TextBlock Opacity="{Binding Opacity, ElementName=image1}" Text="Open Webpage" Tap="EOpenWeb" />
                        <StackPanel Opacity="{Binding Opacity, ElementName=image2}" Orientation="Horizontal" Tap="ECallPhone">
                            <TextBlock Text="Call "/>
                            <TextBlock Text="{Binding Contact.FormattedPhone}"/>
                        </StackPanel>
                        <TextBlock Opacity="{Binding Opacity, ElementName=image3}" Text="On Twitter" Tap="EOpenTwitter" />
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <StackPanel HorizontalAlignment="Stretch">
                    <TextBlock Foreground="{Binding Foreground}"  Text="{Binding Name}" d:LayoutOverrides="HorizontalAlignment"/>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,-6" Visibility="{Binding ShowGlyphs}">
                        <TextBlock TextWrapping="Wrap" Text="{Binding HereNow}" Visibility="{Binding ShowHere}" VerticalAlignment="Center" FontSize="{StaticResource PhoneFontSizeMediumLarge}" HorizontalAlignment="Left" Height="36" Width="48" TextAlignment="Center"/>
                        <Image x:Name="image" Height="48" Opacity="{Binding HasMenu}" Source="locationGlyphs/appbar.book.open.png" Stretch="Fill"/>
                        <Image x:Name="image1" Height="48" Opacity="{Binding HasWeb}" Source="locationGlyphs/appbar.link.png" Stretch="Fill" Width="48"/>
                        <Image x:Name="image2" Height="48" Opacity="{Binding HasPhone}" Source="locationGlyphs/appbar.phone.png" Stretch="Fill" Width="48"/>
                        <Image x:Name="image3" Height="48" Opacity="{Binding HasTwitter}" Source="locationGlyphs/appbar.twitter.bird.png" Stretch="Fill" Width="48"/>
                    </StackPanel>
                </StackPanel>
            </mi:Pushpin>
        </DataTemplate>
    </mi:MapItemsControl.ItemTemplate>
</mi:MapItemsControl>

详细说明了如何将对象绘制到地图上。 Pushpins列表只是我的数据的Observable集合。

如果这是一个ListBox,我可以简单地使用SelectedItem或类似方法解决这个问题,但这似乎不是MapItemsControl的一个选项。有没有人成功地做到了这一点?怎么样?

1 个答案:

答案 0 :(得分:1)

你需要以某种方式对图钉上的水龙头作出反应。你有一个上下文菜单,你显然需要这样做。需要附加一个点击事件处理程序:

<mi:Pushpin
Location="{Binding Location}" Tap="Pushpin_Tap">

需要像下面这样管理Pushpin_Tap事件处理程序:

private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var _ppmodel = sender as Pushpin;
    ContextMenu contextMenu = 
        ContextMenuService.GetContextMenu(_ppmodel);
    contextMenu.DataContext = _viewModel.Pushpins.Where
    (c => (c.Location 
        == _ppmodel.Location)).FirstOrDefault();

    // this way you can find which pushpin/position was tapped by finding the 
    // one which has the same location in your Pushpins observable collection

    if (contextMenu.Parent == null)
    {
        contextMenu.IsOpen = true;
    }
}

我写过关于在我的博客上展示context menu on pushpin tap的文章,所以如果你希望你能在那里阅读整个故事。