megabytes
megabytes

Reputation: 185

WPF menus open right alinged

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

Upvotes: 0

Views: 203

Answers (1)

Alex.Wei
Alex.Wei

Reputation: 1883

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;
}

Upvotes: 1

Related Questions