Yustme
Yustme

Reputation: 6265

Center header in menu control

How do I vertically center a header in the menu control?

This was my try:

<MenuItem Header="File" StaysOpenOnClick="True" FontFamily="Arial" VerticalAlignment="Center">
                <MenuItem Header="Open" Click="Open_Click" IsEnabled="True"/>
            </MenuItem>
        </Menu>

But its aligned to the Top-left.

What am I doing wrong?

[EDIT]

My whole menu now looks like this:

<Menu Canvas.Left="0" Canvas.Top="0" Name="menu1" Margin="0,0,0,384">
        <MenuItem Header="File" StaysOpenOnClick="True" FontFamily="Arial" VerticalAlignment="Center">
            <MenuItem Click="Open_Click" IsEnabled="True">
                <MenuItem.Header>
                    <TextBlock Text="Open" VerticalAlignment="Center"/>
                </MenuItem.Header>
            </MenuItem>
        </MenuItem>
        </Menu>

The header text 'file' still isn't vertically centered (which is what i want to center). What exactly is this code centering? Is it the text 'open'?

[/EDIT]

Upvotes: 6

Views: 7853

Answers (2)

Dan Busha
Dan Busha

Reputation: 3803

If you want to format the header you'll need to explicitly layout the header control:

<MenuItem StaysOpenOnClick="True" FontFamily="Arial" VerticalAlignment="Center">
      <MenuItem Click="Open_Click" IsEnabled="True">
          <MenuItem.Header>
              <TextBlock Text="Open" VerticalAlignment="Center"/>
          </MenuItem.Header>
      </MenuItem>
  </Menu>

Update: To format the position of a MenuItem in the Menu you'll need to override the Menu's ItemsPanelTemplate. By default the Menu uses a vertical WrapPanel which justifies the items to the top. Replace the default with a panel of your choice (StackPanel, Grid, DockPanel, etc) and you'll be able center the menu items as you please. Here's an example:

<Menu Canvas.Left="0" Canvas.Top="0" Name="menu1" Margin="0,0,0,384">
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <MenuItem Header="File" StaysOpenOnClick="True" FontFamily="Arial" VerticalAlignment="Center" >
        <MenuItem Header="Open" Click="Open_Click" IsEnabled="True"/>
    </MenuItem>
</Menu>

Information gathered from this post on MSDN.

Upvotes: 11

brunnerh
brunnerh

Reputation: 185225

I think you would want to set the VerticalContentAlignment. If the aligmnet is still not to your liking there is probably a problem with the default MenuItem Template, it may not bind to the property or there are some margins or Paddings which shift the header.

Upvotes: 0

Related Questions