arjacsoh
arjacsoh

Reputation: 9232

Align menu in wpf

I have a vertical menu set to the left side of the window. Its items open just on (above) it and this prevents the user from having a full view of the menu when an item is open.

I want each element to open just on the right of the menu, so as to have an entire view of both the rest of menu and the opened elements. How can this be done? May be with the aid of transforms or triggers?

Here is some code:

<MenuItem Header="Maths">
    <MenuItem Background="LightGray" Header="Add"/>
    <MenuItem Background="LightGray" Header="Subtract"/>
    <MenuItem Background="LightGray" Header="Multiply"/>
    <MenuItem Background="LightGray" Header="Divide"/>
</MenuItem>

Upvotes: 0

Views: 769

Answers (1)

AndrewS
AndrewS

Reputation: 6094

So just to be clear, the MenuItem 'Maths' above is in a WPF Menu and you have changed that Menu's ItemsPanel to be a Vertical StackPanel or something so 'Maths' is above/below other sibling menu items. If so what is happening is that the default template for MenuItem's whose role is TopLevelHeader (MenuItem that has child Items and is directly within a Menu) is such that the popup is below (or above) the menu item. You will probably want to retemplate those menu items. On hacky (and ugly alternative) is to use the template that would be used for SubmenuHeader role menu items (i.e. a MenuItem that has child Items and is within another MenuItem). e.g.

  <Menu HorizontalAlignment="Left">
    <Menu.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel />
      </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <Menu.ItemContainerStyle>
      <Style TargetType="MenuItem">
        <Style.Triggers>
          <Trigger Property="Role" Value="TopLevelHeader">
            <Setter Property="Template" Value="{DynamicResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}" />
          </Trigger>
          <Trigger Property="Role" Value="TopLevelItem">
            <Setter Property="Template" Value="{DynamicResource {x:Static MenuItem.SubmenuItemTemplateKey}}" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </Menu.ItemContainerStyle>
    <MenuItem Header="Just Item" />
    <MenuItem Header="Maths">
      <MenuItem Header="Add" />
      <MenuItem Header="Subtract" />
    </MenuItem>
    <MenuItem Header="Misc">
      <MenuItem Header="Other" />
    </MenuItem>
  </Menu>

Upvotes: 1

Related Questions