Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:55:20+00:00 2026-06-12T16:55:20+00:00

Does anyone know of how I can determine when the ContextMenu get its placement

  • 0

Does anyone know of how I can determine when the ContextMenu get its placement automatically adjusted due to being too close to the edge of the screen?

My scenario is that I have a ContextMenu that has 2 rounded corners and 2 square corners. When the menu opens down I round the bottom 2, and if the menu is opening upwards then I round the top 2. The problem is that I haven’t found an event or property to bind to that tells me when the menu gets its direction automatically changed.

Here’s some simplified sample code to try out. If you click when the window is at top of screen then menu goes down. If you move window to bottom of screen then the menu will go up.

<Window x:Class="menuRedirection.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="100" Width="200">
  <DockPanel Name="panel" ContextMenuOpening="DockPanel_ContextMenuOpening">
    <DockPanel.ContextMenu>
      <ContextMenu>
        <MenuItem Header="item"/>
        <MenuItem Header="item"/>
        <MenuItem Header="item"/>
        <MenuItem Header="item"/>
      </ContextMenu>
    </DockPanel.ContextMenu>
    <Rectangle DockPanel.Dock="Bottom" Name="menuTarget" Fill="Red" Height="10"/>
    <TextBlock DockPanel.Dock="Top" Text="right click for context menu"/>
  </DockPanel>
</Window>

private void DockPanel_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
  ContextMenuService.SetPlacement(panel, PlacementMode.Bottom);
  ContextMenuService.SetPlacementTarget(panel, menuTarget);
}

Here’s what the real application looks like so you can see my problem with needing to know to adjust my rounded corners.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:55:21+00:00Added an answer on June 12, 2026 at 4:55 pm

    I was unable to find a true WPF solution but Justin’s comment lead me down the path of experimenting with comparing the menu’s location with the PlacementTarget’s location.

    First step was to subscribe to the contextMenu.Loaded event (this fires after layout has been processed but before it’s fully visible on the screen).

    <ContextMenu ContextMenu.Loaded="ContextMenu_Loaded">
    

    And then when that fires I can figure out if the menu was internally switched to the alternate placement for my requested placementMode. If it was reversed then I go ahead and adjust my rounded corners accordingly.

    NOTE: i initially had used getWindowRect and compared the menu Rect with the target’s Rect, but found that the menu Rect was always returning the prior instance’s location. To avoid this problem I now get the relevant screen’s workingArea and manually see if the menu fits.

    NOTE2: be sure your menu’s template results in the same window height for both inverted and regular display. Otherwise, your calculation could be off since getWindowRect returns the last menu’s size.

    void ContextMenu_Loaded(object sender, RoutedEventArgs e)
    {
      bool reversed = isMenuDirectionReversed(this.ContextMenu);
    
      //existing styles are read-only so we have to make a clone to change a property
      if (reversed)
      {//round the top corners if the menu is travelling upward
        Style newStyle = new Style(typeof(ContextMenu), this.ContextMenu.Style);
        newStyle.Setters.Add(new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(10, 10, 0, 0) });
        this.ContextMenu.Style = newStyle;
      }
      else
      { //since we may have overwritten the style in a previous evaluation, 
        //we also need to set the downward corners again    
        Style newStyle = new Style(typeof(ContextMenu), this.ContextMenu.Style);
        newStyle.Setters.Add(new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(0, 0, 10, 10) });
        this.ContextMenu.Style = newStyle;
      }
    }
    

    Evaluation method:

    private bool isMenuDirectionReversed(ContextMenu menu)
    {
      //get the window handles for the popup' placement target
      IntPtr targetHwnd = (HwndSource.FromVisual(menu.PlacementTarget) as HwndSource).Handle;
    
      //get the relevant screen
      winFormsScreen screen = winFormsScreen.FromHandle(targetHwnd);
    
      //get the actual point on screen (workingarea not taken into account)
      FrameworkElement targetCtrl = menu.PlacementTarget as FrameworkElement;
      Point targetLoc = targetCtrl.PointToScreen(new Point(0, 0));
    
      //compute the location for the bottom of the target control
      double targetBottom = targetLoc.Y + targetCtrl.ActualHeight;
    
      if (menu.Placement != PlacementMode.Bottom)
        throw new NotImplementedException("you need to implement your own logic for other modes");
    
      return screen.WorkingArea.Bottom < targetBottom + menu.ActualHeight;
    }
    

    Final result:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how we can get previous versions of z3 for linux 64?
Does anyone know how I can determine if an application is able to accept
Does anyone here know how i can determine the version of SQL running on
does anyone know how one can get the catalog- and cart price rules from
Does anyone know how I can determine the server and share name of a
Does anyone know where I can find an example of how to determine if
Does anyone know I can make min-height work with the latest browsers? I am
Does anyone know how can I cut a string and then assign into an
Does anyone know how can I detect the browser type in css? What I
Does anyone know where can I find recent data on PHP4 vs PHP5 market

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.