WPF Menuitem边框 [英] WPF Menuitem Border

查看:78
本文介绍了WPF Menuitem边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试实现Menu时遇到了问题,无法弄清楚发生了什么.我正在尝试使用Menu控件制作单层菜单.这是我的菜单代码:

I've run into a problem trying to implement a Menu and can't figure out what is going on. I'm trying to make a single-layer menu using the Menu control. Here is my menu code:

<Menu DockPanel.Dock="Top" Height="22" Name="menu1" VerticalAlignment="Top" Background="#FF325170">
    <MenuItem Header="Featured" Style="{StaticResource menuItemStyle}" />
    <MenuItem Header="Search" Style="{StaticResource menuItemStyle}" />
</Menu>

MenuItem的样式如下:

<Style x:Key="menuItemStyle" TargetType="{x:Type MenuItem}">
  <Style.Triggers>
    <Trigger Property="MenuItem.IsMouseOver" Value="true">
      <Setter Property = "Foreground" Value="Red"/>
    </Trigger>
  </Style.Triggers>
</Style>

当我将鼠标悬停在菜单项上时,会出现一个Border,并且我一生都不知道如何删除该边框.有什么建议吗?

When I mouseover the menu items, there is a Border that appears, and I can't figure out for the life of me how to remove this border. Any suggestions?

推荐答案

对于许多内置的WPF控件样式,您需要覆盖ControlTemplate.

For a lot of the built-in WPF control styles, you need to override the ControlTemplate.

这是提供菜单ControlTemplate的MSDN页面,有关如何使用它的说明-基本上,您是在插入所有菜单控件样式的本地副本,然后覆盖默认控件的外观.

Here is the MSDN page that provides the Menu ControlTemplate, with instructions on how to use it -- basically you are inserting local copies of all the styles for the Menu control, which then override the default control look and feel.

要解决您的问题,您应该可以插入以下样式:

To address your problem you should be able to just insert this style:

<Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Setter Property="SnapsToDevicePixels" Value="True"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Menu}">
        <!--Here is where you change the border thickness to zero on the menu-->
        <Border BorderThickness="0">
          <StackPanel ClipToBounds="True" Orientation="Horizontal"
                      IsItemsHost="True"/>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

这篇关于WPF Menuitem边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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