Submenu is not coming up.

Aug 18, 2010 at 6:35 AM

I have the following in my XAML:

        <StackPanel Visibility="Collapsed">
            <ListBox Name="lstConferenceSubMenu" Background="#FF8C9FCB">

            </ListBox>
        </StackPanel>

In my code behind I am building a PopupMenu from scratch:

private PopupMenu listBoxContextmenu = new PopupMenu();

        private void LoadMenu()
        {
            listBoxContextmenu.AddTrigger(TriggerTypes.RightClick, lstStatus);
            listBoxContextmenu.Showing += new EventHandler<MouseEventArgs>(listBoxContext_Showing);
        }

        private void listBoxContext_Showing(object sender, MouseEventArgs e)
        {
                // build a conference menu with a Submenu
                PopupMenuItem mnuConference = listBoxContextmenu.AddItem("Conference Call", delegate
                {
                    Debug.WriteLine("Yeah");
                });
                mnuConference.ImagePathForRightMargin = @"images/arrow.png";
                mnuConference.CloseOnClick = false;

                lstConferenceSubMenu.Items.Clear();

                PopupMenuItem pmiSub;

                pmiSub = new PopupMenuItem();
                pmiSub.Header = "1";
                pmiSub.MouseLeftButtonUp += new MouseButtonEventHandler(pmiSub_MouseLeftButtonUp);
                lstConferenceSubMenu.Items.Add(pmiSub);

                pmiSub = new PopupMenuItem();
                pmiSub.Header = "2";
                pmiSub.MouseLeftButtonUp += new MouseButtonEventHandler(pmiSub_MouseLeftButtonUp);
                lstConferenceSubMenu.Items.Add(pmiSub);

                PopupMenu conferenceSubMenu = new PopupMenu(lstConferenceSubMenu) { ShowOnRight = true };
                conferenceSubMenu.AddTrigger(TriggerTypes.Hover, mnuConference);
                conferenceSubMenu.AddTrigger(TriggerTypes.LeftClick, lstStatus);    // not sure why this is needed
        }

The Conference menu comes up fine, but it never shows the sub menu.  What am I missing?  I've even tried to replicate your sub menu sample, but it did not work seem to work.  Something is off, but I can't tell.

Coordinator
Aug 18, 2010 at 12:47 PM
Edited Aug 18, 2010 at 1:06 PM
        <StackPanel Visibility="Collapsed">
            <ListBox Name="lstConferenceSubMenu" Background="#FF8C9FCB">
            </ListBox>
        </StackPanel>
        <ListBox Name="lstStatus" Width="100" Height="100">
            <ListBoxItem>Status 1</ListBoxItem>
            <ListBoxItem>Status 2</ListBoxItem>
        </ListBox>
 
        private void LoadMenu()
        {
            // Build a conference menu
            PopupMenu pmContextmenu = new PopupMenu();
            // Add an item to the conference menu to reference the submenu
            PopupMenuItem mnuConference = pmContextmenu.AddItem(null"Conference Call "@"images/arrow.png"nulldelegate
            {
                Debug.WriteLine("Yeah");
            });
            mnuConference.CloseOnClick = false;

            // Build a submenu
            PopupMenu pmConferenceSub = new PopupMenu(lstConferenceSubMenu) { ShowOnRight = true };

            // Attach triggers to conference menu and submenu
            pmContextmenu.AddTrigger(TriggerTypes.RightClick, lstStatus);
            pmConferenceSub.AddTrigger(TriggerTypes.Hover, mnuConference);

            // Populate items in submenu while its being shown
            pmContextmenu.Showing += (sender, e) =>
            {
                pmConferenceSub.Items.Clear();
                pmConferenceSub.AddItem("1", pmiSub_MouseLeftButtonUp);
                pmConferenceSub.AddItem("2", pmiSub_MouseLeftButtonUp);
            };
        }

        void pmiSub_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show((sender as TextBlock).Text);
        }
Is that what you were trying to do?

Coordinator
Aug 18, 2010 at 3:07 PM
Edited Aug 18, 2010 at 11:38 PM

Please note that I've added the AddSubMenu and InserSubMenu methods in the latest changeset(http://sl4popupmenu.codeplex.com/SourceControl/list/changesets). The code can now be simplified like this:

         private void Load()
        {
            // Build a submenu            
            PopupMenu pmConferenceSub = new PopupMenu(lstConferenceSubMenu) { ShowOnRight = true };    
            pmConferenceSub.Showing += (sender, e) =>
            {
                pmConferenceSub.Items.Clear();
                pmConferenceSub.AddItem("1", pmiSub_MouseLeftButtonUp);
                pmConferenceSub.AddItem("2", pmiSub_MouseLeftButtonUp);
            };

            // Build a conference menu            
            PopupMenu pmContextmenu = new PopupMenu();
            pmContextmenu.AddSubMenu(pmConferenceSub, "Conference Call ""images/arrow.png"nullnullfalsenull);     
            pmContextmenu.AddTrigger(TriggerTypes.RightClick, lstStatus);

        }


Aug 19, 2010 at 1:57 AM
Edited Aug 19, 2010 at 2:02 AM

Ziad, 

Thanks for your reply.  I still can't get it to work.  So let me give you a better description of the issue. Here is my XAML.  

 

      <ListBox x:Name="lstStatus" Height="197" Grid.ColumnSpan="4" VerticalAlignment="Top" Margin="2,20,17,0" >
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                </Style>
            </ListBox.ItemContainerStyle>            
            
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" 
                                MouseRightButtonDown="StackPanel_MouseRightButtonDown" 
                                MouseRightButtonUp="StackPanel_MouseRightButtonUp">
                        <Image />
                        <TextBlock Width="35" Text="{Binding FriendlyID}" />
                        <TextBlock Width="120" Text="{Binding StatusName}" />
                        <TextBlock Width="70" Text="{Binding LastANI}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <StackPanel Visibility="Collapsed">
            <ListBox Name="lstConferenceSubMenu" Background="#FF8C9FCB">

            </ListBox>
        </StackPanel>

 

The idea is to right click on the lstStatus and on the right-click event determine what should be in the context menu based on what I clicked on.  So to that effect, on page startup, I load up the basic menu:

 

private PopupMenu listBoxContextmenu = new PopupMenu();

private void LoadMenu()
{
    listBoxContextmenu.AddTrigger(TriggerTypes.RightClick, lstStatus);
    listBoxContextmenu.Showing += new EventHandler<MouseEventArgs>(listBoxContext_Showing);
}

 Then on the Showing event of the listBoxContextmenu, I build this menu:

private void listBoxContext_Showing(object sender, MouseEventArgs e)
{
   // remove everything
   for (int i = listBoxContextmenu.Items.Count - 1; i >= 0 ; i--)
      listBoxContextmenu.RemoveAt(i);

  if (answer911CallVisible)
     listBoxContextmenu.AddItem("Answer Call", delegate { HandleAnswer(); });

  if (hangup911CallVisible)
     listBoxContextmenu.AddItem("Hangup Call", delegate { HandleHangup(); });

  if (conference911CallVisible)
  {
     PopupMenuItem mnuConference = listBoxContextmenu.AddItem(null, "Conference Call", @"images/arrow.png", null, delegate
     {
         Debug.WriteLine("Yeah");
     });

     // build a submenu
     PopupMenu pmConferenceSub = new PopupMenu(lstConferenceSubMenu) { ShowOnRight = true };

     // attach triggers 
     pmConferenceSub.AddTrigger(TriggerTypes.Hover, mnuConference);

    // add items to the submenu
    pmConferenceSub.Items.Clear();      
    pmConferenceSub.AddItem("1", pmiSub_MouseLeftButtonUp);      
    pmConferenceSub.AddItem("2", pmiSub_MouseLeftButtonUp);

  }
}

private void pmiSub_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
     MessageBox.Show((sender as TextBlock).Text);
}

 

So in this example, the Conference Call menu shows up fine and so does the image, however, the submenu does not appear ever.  Even if I hard wired items into to lstConferenceSubMenu, it still would not show up.  I am not sure what is missing here.

 

Regards


Coordinator
Aug 21, 2010 at 3:36 AM

I got your point. This is an issue I suggest to add the the issue tracker section. I am investigating on that right now.

Aug 23, 2010 at 5:23 PM

Issue Tracker added.

Oct 27, 2010 at 8:31 PM
Edited Oct 27, 2010 at 8:32 PM

i have same problem. i need dynamic submenus depending on context. submenus not showing :(

Coordinator
Oct 30, 2010 at 1:04 AM

This issue has been tackled on the following link:

http://sl4popupmenu.codeplex.com/workitem/7154