Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

like image 740
Yustme Avatar asked Apr 21 '12 18:04

Yustme


1 Answers

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.

like image 161
Dan Busha Avatar answered Nov 16 '22 16:11

Dan Busha