簡體   English   中英

WPF透明菜單

[英]WPF Transparent menu

我目前有以下菜單:

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

我無法弄清楚如何制作半透明或完全透明的部分,類似浮動文本。 這樣我就可以看到下面的表格了。

任何幫助,將不勝感激。 謝謝!

為此,您需要更改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的簡單樣式作為樣式的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM