0

我对 WPF 比较陌生,我正在尝试更改菜单项的悬停效果并将其设置为类似于工具栏中的按钮之一。

当我最有可能从 MenuItem 悬停在菜单上时,我留下了一个双边框。我怎样才能删除它?

XAML 文件:

     <ToolBarTray Style="{StaticResource MainToolBar}">
            <ToolBar DockPanel.Dock="Top" ToolBarTray.IsLocked="True" Width="Auto" Padding="0" Background="Transparent" Name="Tool">
                <Button Content="Button 1"/>
                <Button Content="Button 2"/>
                <Separator/>
                <Menu Margin="0, -1, 0, 0" Style="{StaticResource Menu}">
                    <MenuItem Header="Menu">
                        <MenuItem Header="File">
                            <MenuItem Header="Copy"/>
                            <MenuItem Header="Paste"/>
                        </MenuItem>
                    </MenuItem>
                </Menu>
            </ToolBar>
           </ToolBarTray>

资源文件:

    <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border
                SnapsToDevicePixels="true"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}">
                    <ContentPresenter
                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" Value="Orange"/>
                        <Setter Property="Background" Value="{DynamicResource ToolBarBKHover}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="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}">
                <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="Orange"/>
                    <Setter Property="Background" Value="{DynamicResource ToolBarBKHover}"/>
                </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

        </Setter.Value>
    </Setter>
</Style>
4

2 回答 2

1

您的“双边框”问题无法在菜单级别解决:您必须进一步挖掘并为菜单的 MenuItem 重新定义模板/触发器。MenuItem (不是Menu)具有将其边框颜色在 MouseOver 上变为灰色的默认行为,这就是您要更改的内容。

编辑:因此,例如,您可以使用 BorderWidth = 0 的设置器为 MenuItem 定义默认样式。

于 2012-07-23T14:50:57.350 回答
0

假设您的菜单项位于 aListbox中,当您移动鼠标时,您希望更改颜色。这是一种简单的方法,但我已经从现有项目中复制了代码:

<Window.Resources>
<Style TargetType="{x:Type ListBox}">
  <Setter Property="Foreground" Value="#58290a" />
  <Setter Property="FontFamily" Value="Lucida Console" />
  <Setter Property="Background">
    <Setter.Value>
      <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
        <LinearGradientBrush.GradientStops>
          <GradientStop Color="#feca00" Offset="0.1"/>
          <GradientStop Color="#ffe100" Offset="0.4"/>
          <GradientStop Color="#feca00" Offset="0.6"/>
          <GradientStop Color="Orange" Offset="0.9"/>
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="BitmapEffect">
        <Setter.Value>
          <OuterGlowBitmapEffect GlowColor="Red" GlowSize="4"/>
        </Setter.Value>
      </Setter>
    </Trigger>
  </Style.Triggers>
</Style>

希望这可以帮助

于 2012-07-23T13:01:58.203 回答