Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
140
Get the corresponding map element on the context menu click or routed command
posted

Hello,

I use the XamMap control with a ValueTemplate for my map elements. I added a context menu in the ValueTemplate of each elements.

I use a Routed Command on the MenuItem but I also tried the click event.

On the click event, the sender and the source are the MenuItem element itself.

On the routed command the sender is the Window and the source is the XamMap control.

How can I retrieved the corresponding map element on the click event or routed command execute method ?

Thanks.

  • 2505
    Verified Answer
    Offline posted

    Hi The_badger_man,

    You could try an approach similar to the following.

    XAML:
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="400"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <ListBox x:Name="lstClickedElement" Height="250" Width="400" Grid.Row="0"/>

            <igMap:XamMap x:Name="map1" Grid.Row="1">
                <igMap:XamMap.Layers>
                    <igMap:MapLayer x:Name="statesLayer">
                        <igMap:MapLayer.Reader>
                            <igMap:ShapeFileReader Uri="/../../Shapefiles/usa/usa_st" DataMapping="Name=STATE_NAME; Value=POP1997; Caption=STATE_NAME"/>
                        </igMap:MapLayer.Reader>

                        <igMap:MapLayer.ValueTemplate>
                            <DataTemplate>
                                <Menu x:Name="menu1" Height="22" Width="100">
                                    <MenuItem Header="Item1" Click="MenuItem_Click"/>
                                </Menu>
                            </DataTemplate>
                        </igMap:MapLayer.ValueTemplate>
                    </igMap:MapLayer>
                </igMap:XamMap.Layers>
            </igMap:XamMap>
        </Grid>

    CB:
            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
                var mnuItem = (MenuItem)sender;
                var element = (MapElement)(mnuItem.DataContext);

                this.lstClickedElement.Items.Add(String.Format("Clicked on {0}.", element.Caption));
            }

    Hope that helps,
    Milana Zhileva