2

I have a WPF application that contains a menu. I need to center the title of the menu:

 <MenuItem Header="_Paramètres" Height="60" Width="188" FontWeight="Bold" FontSize="16" HorizontalContentAlignment="Center"  >
     <MenuItem Header="_Régler" Height="30" Width="188" FontWeight="Bold" FontSize="16" Click="regler_Click_1"  x:Name="regler" Background="#FF150202" HorizontalContentAlignment="Center" />
 </MenuItem>

The menu items are centered but the menu title is not.

How can I do this?

Shlomo
  • 14,102
  • 3
  • 28
  • 43
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191

1 Answers1

1

You should set additionally the HorizontalAlignment of the root MenuItem. Like this.

<MenuItem Header="_Paramètres" Height="60" Width="188" FontWeight="Bold" FontSize="16" 
          HorizontalContentAlignment="Center" HorizontalAlignment="Center" >
    <MenuItem Header="_Régler" Height="30" Width="188" FontWeight="Bold" FontSize="16" 
              Click="regler_Click_1"  x:Name="regler" Background="#FF150202"/>
</MenuItem>

Setting the HorizontalAlignment of the sub MenuItems should not be necessary with this code.

You can find some additional information about HorizontalAlignment and HorizontalContentAlignment in the links.

Edit

Ah ok (Q&A in the comments), then the following could probably help.

<MenuItem Header="_Paramètres" Height="60" Width="188" FontWeight="Bold" FontSize="16" 
          HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" >
    <MenuItem Header="_Régler" Height="30" Width="188" FontWeight="Bold" FontSize="16" 
              Click="regler_Click_1"  x:Name="regler" Background="#FF150202"
              HorizontalAlignment="Stretch" HorizontalContentAlignment="Center"/>
</MenuItem>

Btw you should create a Style, so that you can reuse the settings.

Edit 2

Last idea. If this isn't working, I'll never implement an UI with XAML again. ;o)

<!-- Declare this as resource -->
<Style x:Key="CenteredTextMenuItem" x:TargetType="MenuItem">
    <Setter Property="HeaderTemplate">
        <DataTemplate>
            <TextBox Text={Binding} HorizontalAlignment="Stretch" 
                     HorizontalContentAlignment="Center" FontSize="16" FontWeight="Bold"/>
        </DataTemplate>
    </Setter>
    <Setter Property="Height" Value="30"/>
    <Setter Property="Width" Value="188"/>
</Style>

Usage

<MenuItem Header="_Paramètres" Height="60" Style="{StaticResource CenteredTextMenuItem}" >
    <MenuItem x:Name="regler" Header="_Régler" Click="regler_Click_1"  
              Background="#FF150202" Style="{StaticResource CenteredTextMenuItem}"/>
</MenuItem>
DHN
  • 4,807
  • 3
  • 31
  • 45
  • Hmm odd. One question with *menu title* you mean the text or the whole control? Sorry, I should have asked that at the beginning. – DHN Apr 11 '13 at 13:51