我曾尝试更改多项内容,但无法更改菜单项的颜色。我希望在MenuItem上方生成一些XAML元素,但是我什么也找不到。如果以前已经回答过类似的问题,我会事先道歉,但是我真的找不到任何事情。
<Grid>
<DockPanel Margin="0,0,0,715" >
<Menu DockPanel.Dock="Top" Background="#222222" Height="20" VerticalAlignment="Top">
<MenuItem Header="File" Width="80" Background="#333333" Foreground="White">
<MenuItem Header="_New World" Background="#333333" />
<MenuItem Header="_Open World" Background="#333333" />
<MenuItem Header="_Save World" Background="#333333" />
<Separator/>
<MenuItem Header="_Close World" Background="#333333" />
<MenuItem Header="Exit" Background="#333333" />
</MenuItem>
<MenuItem Header="Edit" Width="80" Background="#333333" Foreground="White">
</MenuItem>
</Menu>
<StackPanel Background="AliceBlue"></StackPanel>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="34" Height="34"/>
</DockPanel>
</Grid>
<Application.Resources>
<Style TargetType="{x:Type StackPanel}"/>
<Style TargetType="{x:Type Menu}">
<Setter Property="Background" Value="#222222"></Setter>
<Setter Property="VerticalAlignment" Value="Top"></Setter>
<Setter Property="Height" Value="20"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
<Style TargetType="{x:Type Grid}"/>
</Application.Resources>
答案 0 :(得分:1)
修改后的答案: 好吧,我现在明白了。实际上,如果您看到menuitem的结构(我使用了snoop应用程序),则在打开menuitem时实际上会看到它的弹出窗口,因此您需要根据需要设置menuitem的模板。我将menuitem的样式属性设置如下:
<Style x:Key="MenuItemBaseStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Grid SnapsToDevicePixels="true">
<DockPanel>
<ContentPresenter x:Name="Icon" ContentSource="Icon" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
<Path x:Name="GlyphPanel" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="7,0,0,0" Visibility="Collapsed" VerticalAlignment="Center"/>
<ContentPresenter x:Name="content" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</DockPanel>
<Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="1" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" VerticalOffset="-1">
<Border BorderThickness="2" BorderBrush="Black" Background="Black">
<ScrollViewer x:Name="SubMenuScrollViewer" CanContentScroll="true" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
</Grid>
</ScrollViewer>
</Border>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextBlock.Foreground" Value="Blue" TargetName="content"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
只需在您的菜单项中实现这种样式:
<MenuItem Header="File" Width="80" Background="Black" Foreground="White" Style="{StaticResource MenuItemBaseStyle}">
答案 1 :(得分:1)
您需要用ControlTemplate
的{{1}}来修改MenuItem
的{{1}}。设置名为“ SubMenuBorder”的Role
元素的TopLevelHeader
属性:
Background
这些值在默认模板中进行了硬编码,因此您不能仅通过设置一些属性来更改它们。您需要定义一个自定义模板。
答案 2 :(得分:0)
您将需要了解有关WPF(实际上是XAML)中的模板和样式的更多信息。在XAML中,控件的外观和控件的操作方式是完全不同的两件事。
阅读详细的答案HERE