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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With