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
785
Constrain PopUp menu within the container
posted

Hi,

 

I'm using a context menu that I attached to a XamWebGrid. It's working fine on Right-Click.

My issue is that I'm using an HTML container on the right-hand side of the grid. If I click on a cell on the right of the grid, then the menu opens and is displayed under the HTML (it's doesn't stay within the grid container).

 

Is there a way to constrain the context menu so that the popup always opens within the parent container? I had a play with the placement properties but they don't do what I want.

 

Cheers.

  • 785
    Verified Answer
    posted

    I'm going to answer my own question.

     I don't know if it's the right way of doing it, but at leadt it works. It took me ages to work out which event I should hook up to. I thought that Opening or Opened were the right ones but it turns out they weren't. SizeChanged does the right thing.

     

    Here's the code that prevents the PopUp from showing outside the right border of the parent control:

     


            private void XamWebContextMenu_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
            {
                var contextMenu = (XamWebContextMenu)sender;
                if (contextMenu.ContextMenuLocation.X + e.NewSize.Width > contextMenu.PlacementTargetResolved.RenderSize.Width)
                {
                    contextMenu.HorizontalOffset += contextMenu.PlacementTargetResolved.RenderSize.Width - (contextMenu.ContextMenuLocation.X + e.NewSize.Width);
                }
            }

            private void XamWebContextMenu_Closed(object sender, System.EventArgs e)
            {
                var contextMenu = (XamWebContextMenu)sender;
                contextMenu.HorizontalOffset = 0;
            }