居中菜单 wpf 的标题 [英] center a title of menu wpf

查看:50
本文介绍了居中菜单 wpf 的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含菜单的 WPF 应用程序.我需要将菜单标题居中:

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.

我该怎么做?

推荐答案

你应该另外设置根MenuItemHorizo​​ntalAlignment.像这样.

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>

此代码不需要设置子MenuItemsHorizo​​ntalAlignment.

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

您可以找到有关Horizo​​ntalAlignmentHorizo​​ntalContentAlignment.

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

编辑

好的(评论中的问答),那么以下内容可能会有所帮助.

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>

顺便说一句,您应该创建一个 Style,以便您可以重复使用这些设置.

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

编辑 2

最后一个想法.如果这不起作用,我将永远不会再使用 XAML 实现 UI.;o)

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>

用法

<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>

这篇关于居中菜单 wpf 的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆