子菜单弹出背景透明
Submenu Popup Background to be Transparent
如何让box/border的子菜单弹出透明?
我设计了菜单和子菜单。 When one selects a menu item that has submenus, one sees a grey box around the styled selections.我该如何设计它?
查看菜单的样式 Menu Styles and Templates 我已尝试对控件 Border
、ScrollViewer
和 Stackpanel
进行多种样式覆盖。我试图覆盖颜色。
<Menu.Resources>
<Style TargetType="Popup"><Setter Property="AllowsTransparency" Value="True" /></Style>
<Style TargetType="ScrollViewer"><Setter Property="Background" Value="Green" /></Style>
<Style TargetType="StackPanel"><Setter Property="Background" Value="Firebrick"/></Style>
<Style TargetType="Border"><Setter Property="Background" Value="Firebrick" /></Style>
<SolidColorBrush x:Key="MenuPopuBrush" Color="Firebrick" />
<SolidColorBrush x:Key=" BorderMediumColor" Color="Firebrick" />
</Menu.Resources>
最小示例
<Menu Foreground="Transparent">
<MenuItem Header="Item 1">
<MenuItem Header="Sub 1a" />
<MenuItem Header="Sub 1b"/>
</MenuItem>
</Menu>
在您的示例中,只需将弹出颜色更改为红色或透明。
不确定这是否是唯一的方法,但您可以覆盖 MenuItem 的 ControlTemplate。基本示例:
<Menu>
<MenuItem Header="Item 1">
<MenuItem.Template>
<ControlTemplate TargetType="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="LightGray" BorderBrush="Transparent" BorderThickness="1" >
<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="LightGray"/>
<Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Firebrick" />
</Trigger>
<Trigger Property="IsHighlighted" Value="False">
<Setter Property="Foreground" Value="Black" />
</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="DarkGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</MenuItem.Template>
<MenuItem Header="Sub 1a" />
<MenuItem Header="Sub 1b" />
</MenuItem>
</Menu>
您现在可以单独访问组件和样式。
如何让box/border的子菜单弹出透明?
我设计了菜单和子菜单。 When one selects a menu item that has submenus, one sees a grey box around the styled selections.我该如何设计它?
查看菜单的样式 Menu Styles and Templates 我已尝试对控件 Border
、ScrollViewer
和 Stackpanel
进行多种样式覆盖。我试图覆盖颜色。
<Menu.Resources>
<Style TargetType="Popup"><Setter Property="AllowsTransparency" Value="True" /></Style>
<Style TargetType="ScrollViewer"><Setter Property="Background" Value="Green" /></Style>
<Style TargetType="StackPanel"><Setter Property="Background" Value="Firebrick"/></Style>
<Style TargetType="Border"><Setter Property="Background" Value="Firebrick" /></Style>
<SolidColorBrush x:Key="MenuPopuBrush" Color="Firebrick" />
<SolidColorBrush x:Key=" BorderMediumColor" Color="Firebrick" />
</Menu.Resources>
最小示例
<Menu Foreground="Transparent">
<MenuItem Header="Item 1">
<MenuItem Header="Sub 1a" />
<MenuItem Header="Sub 1b"/>
</MenuItem>
</Menu>
在您的示例中,只需将弹出颜色更改为红色或透明。
不确定这是否是唯一的方法,但您可以覆盖 MenuItem 的 ControlTemplate。基本示例:
<Menu>
<MenuItem Header="Item 1">
<MenuItem.Template>
<ControlTemplate TargetType="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="LightGray" BorderBrush="Transparent" BorderThickness="1" >
<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="LightGray"/>
<Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Firebrick" />
</Trigger>
<Trigger Property="IsHighlighted" Value="False">
<Setter Property="Foreground" Value="Black" />
</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="DarkGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</MenuItem.Template>
<MenuItem Header="Sub 1a" />
<MenuItem Header="Sub 1b" />
</MenuItem>
</Menu>
您现在可以单独访问组件和样式。