WPF Menu中的文字位置 问题

时间:2023-02-10 20:57:17
WPF Menu中的文字位置 问题
看到了么?文字是在左上角的 有什么方法让它居中呢?

<!-- XAMl中只有定义嗯 大概就是-->
<Menu Name="RootMenu">
    <MenuItem Name="MenuFile"/>
    <MenuItem Name="MenuFileEdit"/>
    <MenuItem Name="MenuHelps"/>
</Menu>

MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.FontSize = 14;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Header = Text;
MenuFile.MouseEnter += (sender, e) => This.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => This.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));


请大神指教。

33 个解决方案

#1


可以自己定义ItemsPanelTemplate:
<Menu Name="RootMenu">
    <!-- 开始添加-->
    <Menu.ItemsPanel >
        <ItemsPanelTemplate>
            <DockPanel HorizontalAlignment="Center" />
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <!-- 结束添加-->
    <MenuItem Name="MenuFile" Header="文件"/>
    <MenuItem Name="MenuFileEdit" Header="编辑"/>
    <MenuItem Name="MenuHelps" Header="帮助"/>
</Menu>


如果你指具体每个菜单,可以自订制Header:

<MenuItem Name="MenuFile">
    <MenuItem.Header>
        <TextBlock Background="Yellow" Width="200" TextAlignment="Center" Text="文件" />
    </MenuItem.Header>
</MenuItem>

#2


如果用textblock显示文字的话,只能用padding来设置文字水平和垂直居中

        <Menu Name="RootMenu">
            <MenuItem Name="MenuFile">
                <MenuItem.Template>
                    <ControlTemplate TargetType="MenuItem">
                        <TextBlock x:Name="txt" Padding="2,2,2,2" Text="文件" Background="White"></TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="txt" Property="Background" Value="Blue"></Setter>
                                <Setter TargetName="txt" Property="Padding" Value="2,2,2,2"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
            <MenuItem Name="MenuFileEdit"/>
            <MenuItem Name="MenuHelps"/>
        </Menu>

#3


HorizontalAlignment=“Center"
VerticalAlignment="Center"

#4



TextBlock.TextAlignment="Center" Padding="5"

#5


引用 1 楼 Forty2 的回复:
可以自己定义ItemsPanelTemplate:
<Menu Name="RootMenu">
    <!-- 开始添加-->
    <Menu.ItemsPanel >
        <ItemsPanelTemplate>
            <DockPanel HorizontalAlignment="Center" />
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <!-- 结束添加-->
    <MenuItem Name="MenuFile" Header="文件"/>
    <MenuItem Name="MenuFileEdit" Header="编辑"/>
    <MenuItem Name="MenuHelps" Header="帮助"/>
</Menu>


如果你指具体每个菜单,可以自订制Header:

<MenuItem Name="MenuFile">
    <MenuItem.Header>
        <TextBlock Background="Yellow" Width="200" TextAlignment="Center" Text="文件" />
    </MenuItem.Header>
</MenuItem>

第一种试了不行 
第二种似乎不显示文本了?

//这堆我都试过了..
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;

#6


引用 2 楼 sunny906 的回复:
如果用textblock显示文字的话,只能用padding来设置文字水平和垂直居中

        <Menu Name="RootMenu">
            <MenuItem Name="MenuFile">
                <MenuItem.Template>
                    <ControlTemplate TargetType="MenuItem">
                        <TextBlock x:Name="txt" Padding="2,2,2,2" Text="文件" Background="White"></TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="txt" Property="Background" Value="Blue"></Setter>
                                <Setter TargetName="txt" Property="Padding" Value="2,2,2,2"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
            <MenuItem Name="MenuFileEdit"/>
            <MenuItem Name="MenuHelps"/>
        </Menu>


//我是这种怎么办?
<Menu Name="RootMenu">
    <MenuItem Name="MenuFile" Header="文件"/>
    <MenuItem Name="MenuFileEdit"/>
    <MenuItem Name="MenuHelps"/>
</Menu>

#7


引用 4 楼 duanzi_peng 的回复:

TextBlock.TextAlignment="Center" Padding="5"

这个似乎是是水平居中 那垂直的呢?

#8


引用 7 楼 u013291805 的回复:
Quote: 引用 4 楼 duanzi_peng 的回复:


TextBlock.TextAlignment="Center" Padding="5"

这个似乎是是水平居中 那垂直的呢?

1楼的方法更好。

#9


引用 8 楼 duanzi_peng 的回复:

//可我Hold不住啊!
//这堆我都试过了!!
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;

#10


引用 1 楼 Forty2 的回复:

唔 第二个方法是可行的
但怎么垂直居中呢?

#11


引用 9 楼 u013291805 的回复:
Quote: 引用 8 楼 duanzi_peng 的回复:

//可我Hold不住啊!
//这堆我都试过了!!
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;


 <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel>
                            <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" Margin="6,0,6,0" VerticalAlignment="Center"/>
                            <Path x:Name="GlyphPanel" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Visibility="Collapsed" VerticalAlignment="Center"/>
                            <!--显示内容-->
                            <Border x:Name="B_MenuItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1">
                                <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

引用:

<MenuItem Header="步骤2" Width="80"  Height="30" Style="{StaticResource myItemStyle}"></MenuItem>

#12


引用 11 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题
为、甚 、麽!!

#13


引用 11 楼 duanzi_peng 的回复:

唔 图片错了..
WPF Menu中的文字位置 问题

#14


引用 13 楼 u013291805 的回复:
Quote: 引用 11 楼 duanzi_peng 的回复:

唔 图片错了..
WPF Menu中的文字位置 问题

咦,怎么回事?设置Header了没有

#15


引用 14 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题
这应该设置了吧?!

#16


引用 15 楼 u013291805 的回复:
Quote: 引用 14 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题
这应该设置了吧?!

对啊。.net framework 版本是4.0的,重新编译试试

#17


引用 16 楼 duanzi_peng 的回复:

改成4.0还是老样子!  
Fill="{TemplateBinding Foreground}" 这句没问题
快要炸毛了!!

#18


引用 16 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题

#19


引用 17 楼 u013291805 的回复:
Quote: 引用 16 楼 duanzi_peng 的回复:

改成4.0还是老样子!  
Fill="{TemplateBinding Foreground}" 这句没问题
快要炸毛了!!

知道哪里错了,手误,把ControlTemplate 改成

 <ControlTemplate TargetType="{x:Type MenuItem}">

#20


引用 19 楼 duanzi_peng 的回复:

悬停事件失效了..
<Window.Resources>
  <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type MenuItem}">
          <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</Window.Resources>


<Menu Name="RootMenu">
  <MenuItem Name="MenuFlie" Style="{StaticResource myItemStyle}"></MenuItem>
</Menu>


MenuFile.Header = 文件;
MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.MouseEnter += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

#21


引用 20 楼 u013291805 的回复:
Quote: 引用 19 楼 duanzi_peng 的回复:

悬停事件失效了..
<Window.Resources>
  <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type MenuItem}">
          <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</Window.Resources>


<Menu Name="RootMenu">
  <MenuItem Name="MenuFlie" Style="{StaticResource myItemStyle}"></MenuItem>
</Menu>


MenuFile.Header = 文件;
MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.MouseEnter += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

参见: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx

#22


该回复于2017-10-31 19:56:04被版主删除

#23


引用 21 楼 duanzi_peng 的回复:
参见: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx

        <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                            <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

还是不行。。

#24


引用 23 楼 u013291805 的回复:
Quote: 引用 21 楼 duanzi_peng 的回复:

参见: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx

        <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                            <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

还是不行。。

用 ControlTemplate Trigger

 <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Border x:Name="B_MenuItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1">
                            <TextBlock x:Name="mytb" Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="B_MenuItem"  Value="Yellow"/>
                                <Setter Property="Foreground" TargetName="mytb" Value="Black"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

#25


引用 24 楼 duanzi_peng 的回复:
WPF Menu中的文字位置 问题

Thx~
最后请教下大神 它还有生育能力 还可以生孩纸麽?
为毛2Menu作为“文件”的下级菜单 却不显示呢。。

#26


引用 25 楼 u013291805 的回复:
Quote: 引用 24 楼 duanzi_peng 的回复:
WPF Menu中的文字位置 问题

Thx~
最后请教下大神 它还有生育能力 还可以生孩纸麽?
为毛2Menu作为“文件”的下级菜单 却不显示呢。。

自己根据需要修改一下。

  <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <DockPanel>
                            <Border x:Name="B_MenuItem" Width="{TemplateBinding Width}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="1">
                                <TextBlock x:Name="mytb" Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                            <Popup x:Name="PART_Popup"  AllowsTransparency="True" Focusable="False" Width="{TemplateBinding Width}"
                                   IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" 
                                   Placement="Bottom"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MaxHeight="150">
                                <Border x:Name="B"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                        <Grid RenderOptions.ClearTypeHint="Enabled">
                                            <!--阴影矩形-->
                                            <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}"/>
                                            <Border BorderBrush="Brown" BorderThickness="1" Background="DarkGray" HorizontalAlignment="Stretch" Width="{TemplateBinding Width}">
                                                <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" KeyboardNavigation.TabNavigation="Cycle"/>
                                            </Border>
                                        </Grid>
                                    </ScrollViewer>
                                </Border>
                            </Popup>
                        </DockPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="B_MenuItem"  Value="Yellow"/>
                                <Setter Property="Foreground" TargetName="mytb" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                                <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                            </Trigger>
                            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True">
                                <Setter Property="Background" TargetName="B" Value="Yellow"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
                                <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
                                <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

#27


引用 26 楼 duanzi_peng 的回复:

定义的Popup是新的么?那原来自身的呢?

WPF Menu中的文字位置 问题
他孙子不像亲生的 
是給二级菜单写个样式 定义二级菜单的Popup?

#28


引用 27 楼 u013291805 的回复:
Quote: 引用 26 楼 duanzi_peng 的回复:

定义的Popup是新的么?那原来自身的呢?

WPF Menu中的文字位置 问题
他孙子不像亲生的 
是給二级菜单写个样式 定义二级菜单的Popup?

孙子也一样引用。

#29


引用 28 楼 duanzi_peng 的回复:

孙子不行!
WPF Menu中的文字位置 问题

WPF Menu中的文字位置 问题
这是鼠标放上去时的

#30


引用 29 楼 u013291805 的回复:
Quote: 引用 28 楼 duanzi_peng 的回复:

孙子不行!
WPF Menu中的文字位置 问题

WPF Menu中的文字位置 问题
这是鼠标放上去时的

那你再从新写个MenuItem样式,专门用于孙子项的引用。

#31


引用 30 楼 duanzi_peng 的回复:

我的理解是 弹出的框是Popup 孙子所在的框是儿子的 
所以我尝试了給儿子套用样式 然后就成了这样。。
WPF Menu中的文字位置 问题WPF Menu中的文字位置 问题

我在纠结 横向的是Popup麽?
我知道应该滚去看书 可是书还没到。。

#32


引用 31 楼 u013291805 的回复:
Quote: 引用 30 楼 duanzi_peng 的回复:

我的理解是 弹出的框是Popup 孙子所在的框是儿子的 
所以我尝试了給儿子套用样式 然后就成了这样。。
WPF Menu中的文字位置 问题WPF Menu中的文字位置 问题

我在纠结 横向的是Popup麽?
我知道应该滚去看书 可是书还没到。。

或者你先学会使用Blend 

#33


引用 32 楼 duanzi_peng 的回复:
或者你先学会使用Blend 

这就麻烦啦!因为重写窗体 向下还原 最大化的按钮图片需要替换 之类的,我在XAML中几乎只写了声明.. 总感觉

#1


可以自己定义ItemsPanelTemplate:
<Menu Name="RootMenu">
    <!-- 开始添加-->
    <Menu.ItemsPanel >
        <ItemsPanelTemplate>
            <DockPanel HorizontalAlignment="Center" />
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <!-- 结束添加-->
    <MenuItem Name="MenuFile" Header="文件"/>
    <MenuItem Name="MenuFileEdit" Header="编辑"/>
    <MenuItem Name="MenuHelps" Header="帮助"/>
</Menu>


如果你指具体每个菜单,可以自订制Header:

<MenuItem Name="MenuFile">
    <MenuItem.Header>
        <TextBlock Background="Yellow" Width="200" TextAlignment="Center" Text="文件" />
    </MenuItem.Header>
</MenuItem>

#2


如果用textblock显示文字的话,只能用padding来设置文字水平和垂直居中

        <Menu Name="RootMenu">
            <MenuItem Name="MenuFile">
                <MenuItem.Template>
                    <ControlTemplate TargetType="MenuItem">
                        <TextBlock x:Name="txt" Padding="2,2,2,2" Text="文件" Background="White"></TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="txt" Property="Background" Value="Blue"></Setter>
                                <Setter TargetName="txt" Property="Padding" Value="2,2,2,2"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
            <MenuItem Name="MenuFileEdit"/>
            <MenuItem Name="MenuHelps"/>
        </Menu>

#3


HorizontalAlignment=“Center"
VerticalAlignment="Center"

#4



TextBlock.TextAlignment="Center" Padding="5"

#5


引用 1 楼 Forty2 的回复:
可以自己定义ItemsPanelTemplate:
<Menu Name="RootMenu">
    <!-- 开始添加-->
    <Menu.ItemsPanel >
        <ItemsPanelTemplate>
            <DockPanel HorizontalAlignment="Center" />
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <!-- 结束添加-->
    <MenuItem Name="MenuFile" Header="文件"/>
    <MenuItem Name="MenuFileEdit" Header="编辑"/>
    <MenuItem Name="MenuHelps" Header="帮助"/>
</Menu>


如果你指具体每个菜单,可以自订制Header:

<MenuItem Name="MenuFile">
    <MenuItem.Header>
        <TextBlock Background="Yellow" Width="200" TextAlignment="Center" Text="文件" />
    </MenuItem.Header>
</MenuItem>

第一种试了不行 
第二种似乎不显示文本了?

//这堆我都试过了..
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;

#6


引用 2 楼 sunny906 的回复:
如果用textblock显示文字的话,只能用padding来设置文字水平和垂直居中

        <Menu Name="RootMenu">
            <MenuItem Name="MenuFile">
                <MenuItem.Template>
                    <ControlTemplate TargetType="MenuItem">
                        <TextBlock x:Name="txt" Padding="2,2,2,2" Text="文件" Background="White"></TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="txt" Property="Background" Value="Blue"></Setter>
                                <Setter TargetName="txt" Property="Padding" Value="2,2,2,2"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </MenuItem.Template>
            </MenuItem>
            <MenuItem Name="MenuFileEdit"/>
            <MenuItem Name="MenuHelps"/>
        </Menu>


//我是这种怎么办?
<Menu Name="RootMenu">
    <MenuItem Name="MenuFile" Header="文件"/>
    <MenuItem Name="MenuFileEdit"/>
    <MenuItem Name="MenuHelps"/>
</Menu>

#7


引用 4 楼 duanzi_peng 的回复:

TextBlock.TextAlignment="Center" Padding="5"

这个似乎是是水平居中 那垂直的呢?

#8


引用 7 楼 u013291805 的回复:
Quote: 引用 4 楼 duanzi_peng 的回复:


TextBlock.TextAlignment="Center" Padding="5"

这个似乎是是水平居中 那垂直的呢?

1楼的方法更好。

#9


引用 8 楼 duanzi_peng 的回复:

//可我Hold不住啊!
//这堆我都试过了!!
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;

#10


引用 1 楼 Forty2 的回复:

唔 第二个方法是可行的
但怎么垂直居中呢?

#11


引用 9 楼 u013291805 的回复:
Quote: 引用 8 楼 duanzi_peng 的回复:

//可我Hold不住啊!
//这堆我都试过了!!
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;


 <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel>
                            <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" Margin="6,0,6,0" VerticalAlignment="Center"/>
                            <Path x:Name="GlyphPanel" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Visibility="Collapsed" VerticalAlignment="Center"/>
                            <!--显示内容-->
                            <Border x:Name="B_MenuItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1">
                                <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

引用:

<MenuItem Header="步骤2" Width="80"  Height="30" Style="{StaticResource myItemStyle}"></MenuItem>

#12


引用 11 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题
为、甚 、麽!!

#13


引用 11 楼 duanzi_peng 的回复:

唔 图片错了..
WPF Menu中的文字位置 问题

#14


引用 13 楼 u013291805 的回复:
Quote: 引用 11 楼 duanzi_peng 的回复:

唔 图片错了..
WPF Menu中的文字位置 问题

咦,怎么回事?设置Header了没有

#15


引用 14 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题
这应该设置了吧?!

#16


引用 15 楼 u013291805 的回复:
Quote: 引用 14 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题
这应该设置了吧?!

对啊。.net framework 版本是4.0的,重新编译试试

#17


引用 16 楼 duanzi_peng 的回复:

改成4.0还是老样子!  
Fill="{TemplateBinding Foreground}" 这句没问题
快要炸毛了!!

#18


引用 16 楼 duanzi_peng 的回复:

WPF Menu中的文字位置 问题

#19


引用 17 楼 u013291805 的回复:
Quote: 引用 16 楼 duanzi_peng 的回复:

改成4.0还是老样子!  
Fill="{TemplateBinding Foreground}" 这句没问题
快要炸毛了!!

知道哪里错了,手误,把ControlTemplate 改成

 <ControlTemplate TargetType="{x:Type MenuItem}">

#20


引用 19 楼 duanzi_peng 的回复:

悬停事件失效了..
<Window.Resources>
  <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type MenuItem}">
          <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</Window.Resources>


<Menu Name="RootMenu">
  <MenuItem Name="MenuFlie" Style="{StaticResource myItemStyle}"></MenuItem>
</Menu>


MenuFile.Header = 文件;
MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.MouseEnter += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

#21


引用 20 楼 u013291805 的回复:
Quote: 引用 19 楼 duanzi_peng 的回复:

悬停事件失效了..
<Window.Resources>
  <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type MenuItem}">
          <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</Window.Resources>


<Menu Name="RootMenu">
  <MenuItem Name="MenuFlie" Style="{StaticResource myItemStyle}"></MenuItem>
</Menu>


MenuFile.Header = 文件;
MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.MouseEnter += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

参见: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx

#22


该回复于2017-10-31 19:56:04被版主删除

#23


引用 21 楼 duanzi_peng 的回复:
参见: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx

        <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                            <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

还是不行。。

#24


引用 23 楼 u013291805 的回复:
Quote: 引用 21 楼 duanzi_peng 的回复:

参见: http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx

        <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                            <TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

还是不行。。

用 ControlTemplate Trigger

 <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Border x:Name="B_MenuItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1">
                            <TextBlock x:Name="mytb" Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="B_MenuItem"  Value="Yellow"/>
                                <Setter Property="Foreground" TargetName="mytb" Value="Black"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

#25


引用 24 楼 duanzi_peng 的回复:
WPF Menu中的文字位置 问题

Thx~
最后请教下大神 它还有生育能力 还可以生孩纸麽?
为毛2Menu作为“文件”的下级菜单 却不显示呢。。

#26


引用 25 楼 u013291805 的回复:
Quote: 引用 24 楼 duanzi_peng 的回复:
WPF Menu中的文字位置 问题

Thx~
最后请教下大神 它还有生育能力 还可以生孩纸麽?
为毛2Menu作为“文件”的下级菜单 却不显示呢。。

自己根据需要修改一下。

  <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="60"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <DockPanel>
                            <Border x:Name="B_MenuItem" Width="{TemplateBinding Width}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="1">
                                <TextBlock x:Name="mytb" Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                            <Popup x:Name="PART_Popup"  AllowsTransparency="True" Focusable="False" Width="{TemplateBinding Width}"
                                   IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" 
                                   Placement="Bottom"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MaxHeight="150">
                                <Border x:Name="B"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                        <Grid RenderOptions.ClearTypeHint="Enabled">
                                            <!--阴影矩形-->
                                            <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}"/>
                                            <Border BorderBrush="Brown" BorderThickness="1" Background="DarkGray" HorizontalAlignment="Stretch" Width="{TemplateBinding Width}">
                                                <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" KeyboardNavigation.TabNavigation="Cycle"/>
                                            </Border>
                                        </Grid>
                                    </ScrollViewer>
                                </Border>
                            </Popup>
                        </DockPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="B_MenuItem"  Value="Yellow"/>
                                <Setter Property="Foreground" TargetName="mytb" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                                <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                            </Trigger>
                            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True">
                                <Setter Property="Background" TargetName="B" Value="Yellow"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
                                <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
                                <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

#27


引用 26 楼 duanzi_peng 的回复:

定义的Popup是新的么?那原来自身的呢?

WPF Menu中的文字位置 问题
他孙子不像亲生的 
是給二级菜单写个样式 定义二级菜单的Popup?

#28


引用 27 楼 u013291805 的回复:
Quote: 引用 26 楼 duanzi_peng 的回复:

定义的Popup是新的么?那原来自身的呢?

WPF Menu中的文字位置 问题
他孙子不像亲生的 
是給二级菜单写个样式 定义二级菜单的Popup?

孙子也一样引用。

#29


引用 28 楼 duanzi_peng 的回复:

孙子不行!
WPF Menu中的文字位置 问题

WPF Menu中的文字位置 问题
这是鼠标放上去时的

#30


引用 29 楼 u013291805 的回复:
Quote: 引用 28 楼 duanzi_peng 的回复:

孙子不行!
WPF Menu中的文字位置 问题

WPF Menu中的文字位置 问题
这是鼠标放上去时的

那你再从新写个MenuItem样式,专门用于孙子项的引用。

#31


引用 30 楼 duanzi_peng 的回复:

我的理解是 弹出的框是Popup 孙子所在的框是儿子的 
所以我尝试了給儿子套用样式 然后就成了这样。。
WPF Menu中的文字位置 问题WPF Menu中的文字位置 问题

我在纠结 横向的是Popup麽?
我知道应该滚去看书 可是书还没到。。

#32


引用 31 楼 u013291805 的回复:
Quote: 引用 30 楼 duanzi_peng 的回复:

我的理解是 弹出的框是Popup 孙子所在的框是儿子的 
所以我尝试了給儿子套用样式 然后就成了这样。。
WPF Menu中的文字位置 问题WPF Menu中的文字位置 问题

我在纠结 横向的是Popup麽?
我知道应该滚去看书 可是书还没到。。

或者你先学会使用Blend 

#33


引用 32 楼 duanzi_peng 的回复:
或者你先学会使用Blend 

这就麻烦啦!因为重写窗体 向下还原 最大化的按钮图片需要替换 之类的,我在XAML中几乎只写了声明.. 总感觉