0

I have a top menu bar that has several drop down menus.

Currently when you open a menu, its child menu opens aligned on the left. Is there a way I can shift that alignment to the right. I have read a few things where I could change the system properties from left to right handed etc, but I just want this case to be purely for this particular menu.

Example:

From this

enter image description here

To this

enter image description here

megabytes
  • 115
  • 2
  • 13

1 Answers1

1

Here is a way to achieve what you want. Assume there is a menu like:

<Menu>
    <MenuItem Header="AAA" SubmenuOpened="MenuItem_SubmenuOpened">
        <MenuItem Header="111"/>
    </MenuItem>
</Menu>

Then in code-behind, we can adjust the Popup of sub-menu like:

private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
    MenuItem owner = (MenuItem)sender;
    Popup child = (Popup)owner.Template.FindName("PART_Popup", owner);

    child.Placement = PlacementMode.Left;
    child.HorizontalOffset = owner.ActualWidth;
    child.VerticalOffset = owner.ActualHeight;
}
Alex.Wei
  • 1,798
  • 6
  • 12
  • @ megabytes Glad to help. I just found out there a mistake that will cause unnecessary measuring. I will correct my answer. – Alex.Wei Jul 04 '18 at 07:37
  • i've found that removing the: if(!child.Child.IsMeasureValid) { child.Child.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); } code, there is a delay in the correct positioning of the menu. It doesn't appear to update until I actually mouse over the menu itself - which i do kinda find odd. – megabytes Jul 06 '18 at 04:46