代码之家  ›  专栏  ›  技术社区  ›  tcables

WPF透明菜单

  •  1
  • tcables  · 技术社区  · 15 年前

    我现在有以下菜单:

            <Menu Grid.Row="1" Margin="3" Background="Transparent">
            <MenuItem Name="mnuFile" Header="File" Background="#28FFAE04" Foreground="#FFFED528">
                <MenuItem Name="mnuSettings" Header="Settings" Background="#28FFAE04" Foreground="#FFFED528" />
                <MenuItem Name="mnuExit" Header="Exit" Background="#28FFAE04" Foreground="#FFFED528" />
            </MenuItem>
            <MenuItem Name="mnuView" Header="View" Background="#28FFAE04" Foreground="#FFFED528" />
            <MenuItem Name="mnuAbout" Header="About" Background="#28FFAE04" Foreground="#FFFED528"  />
        </Menu>
    

    我想不出如何使掉下来的部分像漂浮的文字一样半透明或完全透明。以便我能看到下面的表格。

    任何帮助都将不胜感激。 谢谢!

    1 回复  |  直到 15 年前
        1
  •  4
  •   robertos    15 年前

    为此,您需要更改MenuItem模板。实现所需功能的最简单的模板更改如下:

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

    Blend 编辑样式/模板或使用 Kaxaml 的简单样式作为样式的起点。