ContextMenu on MapPolylines does not work

2 posts, 0 answers
  1. Padmanabhan
    Padmanabhan avatar
    4 posts
    Member since:
    Jan 2011

    Posted 18 Feb 2011 Link to this post

    Hi,

       I'm trying to add Context Menu to MapPolylines and somehow it is not displaying the context menu. My requirement is to Right Click on MapPolyline and select Hide Polyline from Context Menu. Please see the code below:

    Registering this event when drawing Polyline

     

     

    private void techpolyline_MouseRightButtonDown(object sender, MouseButtonEventArgs e)

     

     

    {

     

     

     

    MapPolyline rightClickPolyline;

     

     

     

     

    ContextMenu mainMenu = new ContextMenu();

     

     

    mainMenu.Background =

     

    Brushes.LightBlue;

     

     

    mainMenu.Height = 50;

     

    mainMenu.Width = 120;

     

     

     

    MenuItem baseItem = new MenuItem();

     

     

    baseItem.Width = 120;

     

    baseItem.Header =

     

    "Select Action";

     

     

    baseItem.Background =

     

    Brushes.Blue;

     

     

    baseItem.Foreground =

     

    Brushes.White;

     

     

    baseItem.FontWeight =

     

    FontWeights.Bold;

     

     

    mainMenu.Items.Add(baseItem);

     

     

     

     

     

    MenuItem hidepolygonItem = new MenuItem();

     

     

    hidepolygonItem.Width = 120;

     

    hidepolygonItem.Header =

     

    "Hide Polyline";

     

     

    mainMenu.Items.Add(hidepolygonItem);

     

     

     

    //hidepolygonItem.PreviewMouseLeftButtonDown +=new MouseButtonEventHandler(hidepolygonItem_PreviewMouseLeftButtonDown);

     

     

     

    if (sender.GetType() == typeof(MapPolyline))

     

     

    {

     

      rightClickPolyline = (

     

    MapPolyline)sender;

     

     

      rightClickPolyline.ContextMenu = mainMenu;

     

    }

     

    }


    I use the same logic to MapPinPoint items and Pushpin items and it works. Any help is appreciated.

    Regards!!
    Paddy
  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 23 Feb 2011 Link to this post

    Hi Padmanabhan,

    Presently, the map shape objects do not support context menu. But you can use another technique to show a context menu:

    <telerik:RadMap x:Name="RadMap1" 
            Center="37,-100.1076291138805"
            ZoomLevel="3"
            ContextMenuOpening="RadMapContextMenuOpening">
        <telerik:RadMap.Provider>
            <telerik:OpenStreetMapProvider />
        </telerik:RadMap.Provider>
        <telerik:RadMap.ContextMenu>
            <ContextMenu Name="contextMenu">
            </ContextMenu>
        </telerik:RadMap.ContextMenu>
        <telerik:InformationLayer x:Name="InformationLayer" />
    </telerik:RadMap>

    private Location menuLocation = Location.Empty;
      
    private void MenuItemClicked(object sender, RoutedEventArgs e)
    {
        if (!this.menuLocation.IsEmpty)
        {
            IEnumerable<object> list = this.InformationLayer.GetItemsInLocation(this.menuLocation);
            foreach (object item in list)
            {
                MapShape shape = item as MapShape;
                if (shape != null)
                {
                    // Do your things here
                    break;
                }
            }
        }
    }
      
    private void RadMapContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
        this.menuLocation = Location.GetCoordinates(this.RadMap1, Mouse.GetPosition(this.RadMap1));
      
        bool close = true;
        IEnumerable<object> list = this.InformationLayer.GetItemsInLocation(this.menuLocation);
        foreach (object item in list)
        {
            MapShape shape = item as MapShape;
            if (shape != null)
            {
                close = false;
                break;
            }
        }
      
        if (!close)
        {
            this.SetupContextMenuItems(this.contextMenu);
        }
      
        e.Handled = close;
    }
      
    private void SetupContextMenuItems(ContextMenu menu)
    {
        menu.Items.Clear();
      
        MenuItem baseItem = new MenuItem();
        baseItem.Width = 200;
        baseItem.Header = "Select Action";
        baseItem.Background = Brushes.Blue;
        baseItem.Foreground = Brushes.White;
        baseItem.FontWeight = FontWeights.Bold;
        baseItem.Click += new RoutedEventHandler(this.MenuItemClicked);
        menu.Items.Add(baseItem);
      
        MenuItem hidepolygonItem = new MenuItem();
        hidepolygonItem.Width = 200;
        hidepolygonItem.Header = "Hide Polyline";
        menu.Items.Add(hidepolygonItem);
    }


    Greetings,
    Andrey Murzov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top