locked
Windows 8 Context Menu / Popup Menu Interaction Problem RRS feed

  • Question

  • I am having an odd problem when using a popup in conjunction with a context menu (popupmenu).  Below, I've posted what I believe is a minimal example of this behavior (XAML and .cs provided for the MainPage file).  This is my first foray into Windows development, so be patient with any stupid mistakes I am making; I'm sure this is just a simple mistake!

    When the context menu is initially displayed, all works as expected -- the items on the context menu can be selected.  However, after a popup is displayed and then closed programmatically (e.g., with a button in that popup setting the popup's 'IsOpen' property to false), the context menu no longer works as expected.  Instead, when the right click triggers a context menu, the menu displays but moving the mouse over an item does not cause that item to change background color (e.g., 'hover' behavior), and the items are not selectable.

    As an interesting additional datapoint: when the popup is closed through the "lightweight dismissal" (clicking elsewhere) rather than setting "IsOpen" to false diresctly, the context menu works as expected.

    To reproduce via the code below:

    1) Right click anywhere on the red canvas; note that a context menu pops up with one item.  Selecting that item changes the text in a textblock at the top left.

    2) Click on the "Open Popup" button.

    3) Click on the "Close Me" button.

    4) Right click on the canvas as before; note that a context menu pops up but its contents are not selectable.

    5) To restore context menu functionality, click on the 'open popup', then click elsewhere on the page to trigger a lightweight dismissal of the popup.  Right click on the canvas and observe a properly functioning context menu.

    Thanks for your time,

    Matt

    <Page
        x:Class="App2.MainPage"
        IsTabStop="false"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Canvas x:Name="MyCanvas" Background="Red" RightTapped="Canvas_RightTapped">
                <TextBlock x:Name="DebugText"></TextBlock>
                <Button x:Name="PopupOpener" Click="PopupOpener_Click" Margin="0,40">OPEN POPUP</Button>
            </Canvas>
            <Popup x:Name="MyPopup" VerticalOffset="80" IsLightDismissEnabled="true">
                <Button x:Name="CloseMe" Click="CloseMe_Click">CLOSE ME</Button>
            </Popup>
        </Grid>
    </Page>

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace App2
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
            private async void Canvas_RightTapped(object sender, RightTappedRoutedEventArgs e)
            {
                DebugText.Text = "Context Menu Opened";
                Rect r = new Rect(e.GetPosition(MyCanvas).X,e.GetPosition(MyCanvas).Y,0,0);
                PopupMenu menu = new PopupMenu();
                menu.Commands.Add(new UICommand("Testing", (command) =>
                {
                    DebugText.Text = "Context Menu Item Selected";
                }));
                await menu.ShowForSelectionAsync(r);
            }
    
            private void CloseMe_Click(object sender, RoutedEventArgs e)
            {
                DebugText.Text = "Closing the popup";
                MyPopup.IsOpen = false;
            }
            
            private void PopupOpener_Click(object sender, RoutedEventArgs e)
            {
                DebugText.Text = "Opening the popup";
                MyPopup.IsOpen = true;
            }
        }
    }
    

    Monday, November 5, 2012 3:00 AM

Answers

  • This seems to have been fixed after an upgrade to RTM from the release candidate.
    • Marked as answer by RoboMatt Wednesday, November 7, 2012 4:11 AM
    Wednesday, November 7, 2012 4:11 AM