MenuItem背景不透明但不是它的文字?

时间:2011-08-10 13:18:01

标签: c# .net wpf xaml menuitem

如何将menuitem的背景不透明度设置为0.0但仍显示其1.0不透明度的文本?

2 个答案:

答案 0 :(得分:3)

要执行此操作,您需要将背景颜色设置为没有Alpha通道的颜色(十六进制颜色的前两位数字0x00000000)。这将使背景颜色完全透明。然后,您的文本只需要为Alpha通道的任何颜色为0xFF(默认值)。

答案 1 :(得分:2)

请参阅此问题:WPF Transparent menu

您需要覆盖MenuItem模板。将其复制/粘贴到您的Window.ResourcesMenu.Resources

<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
    <Border Name="Border" >
      <Grid>
        <ContentPresenter 
          Margin="6,3,6,3" 
          ContentSource="Header"
          RecognizesAccessKey="True" />
        <Popup 
          Name="Popup"
          Placement="Bottom"
          IsOpen="{TemplateBinding IsSubmenuOpen}"
          AllowsTransparency="True" 
          Focusable="False"
          PopupAnimation="Fade">
          <Border 
            Name="SubmenuBorder"
            SnapsToDevicePixels="True"
            Background="Transparent">
            <StackPanel  
              IsItemsHost="True" 
              KeyboardNavigation.DirectionalNavigation="Cycle" />
          </Border>
        </Popup>
      </Grid>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="IsSuspendingPopupAnimation" Value="true">
        <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/>
      </Trigger>
      <Trigger Property="IsHighlighted" Value="true">
        <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/>
        <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
      </Trigger>
      <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
        <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/>
        <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="#888888"/>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>