locked
context menu for the gridviewitem RRS feed

  • Question

  • Hi All,

    Metro app windows application development :

    I have the gridview and context menu(popupmenu) with modify & delete items. On rightclick of the gridview item, this popup gets displayed. This is displayed always on the top not exactly near the gridviewitem.
    Please let me know how i can show the popupmenu near the item.


       private async void gvProgKey_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
                if (e.AddedItems.Count != 0)  // meena sep bug fix
                {
                    PopupMenu p = new PopupMenu();

                    p.Commands.Add(new UICommand("Modify", new UICommandInvokedHandler(this.ModifyProgKey), gvProgKey.SelectedIndex));

                    p.Commands.Add(new UICommand("Delete", new UICommandInvokedHandler(this.Deleteprogkey), gvProgKey.SelectedIndex));

                    await p.ShowForSelectionAsync(GetRect(sender), Placement.Default);

                   gvProgKey.SelectedIndex = -1;

                }
    }
    private Rect GetRect(object sender)
            {

                FrameworkElement element = sender as FrameworkElement;

                GeneralTransform elementTransform = element.TransformToVisual(null);

                Point point = elementTransform.TransformPoint(new Point(0,0));

                return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
    }

    Thanks,

    Meena

    Monday, September 16, 2013 11:35 AM

Answers

  • Hi Meena,

    You have the right idea here, but you need to be able to find the selected GridViewItem so you can call ShowForSelectionAsync on its rectangle. This is tricky to do in Windows 8. The Windows 8.1 Preview adds the ContainerFromItem method to map from the SelectedItem back to its container.

    You can handle this mapping yourself on Windows 8 (I'd start by looking at storing it off in PrepareContainerForItemOverride) or you can approximate the location by using the mouse or touch location if such is available or guessing somewhere reasonable if not.

    --Rob

    • Marked as answer by Anne Jing Friday, September 20, 2013 1:39 AM
    Tuesday, September 17, 2013 1:33 AM
    Moderator

All replies

  • Hi Meena,

    You have the right idea here, but you need to be able to find the selected GridViewItem so you can call ShowForSelectionAsync on its rectangle. This is tricky to do in Windows 8. The Windows 8.1 Preview adds the ContainerFromItem method to map from the SelectedItem back to its container.

    You can handle this mapping yourself on Windows 8 (I'd start by looking at storing it off in PrepareContainerForItemOverride) or you can approximate the location by using the mouse or touch location if such is available or guessing somewhere reasonable if not.

    --Rob

    • Marked as answer by Anne Jing Friday, September 20, 2013 1:39 AM
    Tuesday, September 17, 2013 1:33 AM
    Moderator
  • Hi Rob,

    I have found the solution to display the context menu at the proper location.

    I have added the event Pointermoved as below and got the coordinates. and changed the GetRect()

    private void gvProgKey_PointerMoved_1(object sender, PointerRoutedEventArgs e)
            {
                XAxis = Convert.ToDouble(e.GetCurrentPoint(null).Position.X);
               YAxis = Convert.ToDouble(e.GetCurrentPoint(null).Position.Y);
            }


    return new Rect(XAxis ,YAxis,element.ActualWidth ,element.ActualHeight);

     

    It works fine for both touch and mouse.

     Thanks

    Meena

     

    Tuesday, September 24, 2013 11:59 AM
  • Hi Meena,

    I'm glad to hear it's working. Thanks for the update!

    --Rob

    Tuesday, September 24, 2013 4:28 PM
    Moderator