比较来自世界各地的卖家的域名和 IT 服务价格

WPF C#-Changing刷菜单背景

有谁知道如何更改菜单背景的刷子? 这听起来很简单,但我看不到任何明显的方法。 你可能会认为这个财产 Background 改变它,但这不是。

这就是我的菜单的样子。 /默认情况下注意白色背景/:

示例代码

:


<window height="300" title="Window1" width="300" x:class="WpfApplication1.Window1" xmlns="[url=http://schemas.microsoft.com/winfx/2006/xaml/presentation"]http://schemas.microsoft.com/w ... ot%3B[/url] xmlns:x="[url=http://schemas.microsoft.com/winfx/2006/xaml">]http://schemas.microsoft.com/w ... gt%3B[/url]
<window.resources>
<solidcolorbrush color="#FF505050" x:key="menuItemBrush"></solidcolorbrush>
</window.resources>
<grid background="#FF252525" x:name="mainGrid">
<menu background="{DynamicResource menuItemBrush}" grid.columnspan="2" verticalalignment="Top" x:name="mainMenu">
<menuitem background="{DynamicResource menuItemBrush}" foreground="White" header="File" x:name="fileMenu"/>
<menuitem background="{DynamicResource menuItemBrush}" foreground="White" header="Open..." x:name="fileOpenMenuItem">
<menuitem background="{DynamicResource menuItemBrush}" foreground="White" header="Save" x:name="fileSaveMenuItem"/>
<menuitem background="{DynamicResource menuItemBrush}" foreground="White" header="Save As..." x:name="fileSaveAsMenuItem"/>
<separator style="{DynamicResource menuItemSeperator}"></separator>
<menuitem background="{DynamicResource menuItemBrush}" foreground="White" header="Exit" x:name="fileExitMenuItem"/>
</menuitem>
</menu>
</grid>


</window>
已邀请:

君笑尘

赞同来自:

我建议使用一个名为的工具 kaxaml 用于写作风格。 它包括多个代码片段,选择颜色和好方法,即立即查看代码的更改发生的情况 xaml. 以下是您想要做的完整样式菜单。 只需更改顶部刷子的颜色,以便它们符合您的需求。


<page xmlns="[url=http://schemas.microsoft.com/winfx/2006/xaml/presentation"]http://schemas.microsoft.com/w ... ot%3B[/url] xmlns:x="[url=http://schemas.microsoft.com/winfx/2006/xaml">]http://schemas.microsoft.com/w ... gt%3B[/url]
<page.resources>
<!--**************************
* STYLE: MenuItem
************************** -->
<solidcolorbrush color="#FF797878" x:key="HighlightedBackgroundBrush"></solidcolorbrush>
<solidcolorbrush color="#FF505050" x:key="MenuBackgroundBrush"></solidcolorbrush>
<solidcolorbrush color="#FFFFFFFF" x:key="NormalBorderBrush"></solidcolorbrush>
<solidcolorbrush color="#FFFFFFFF" x:key="SolidMenuFontBrush"></solidcolorbrush>
<solidcolorbrush color="#FFFFFFFF" x:key="HighlightedText"></solidcolorbrush>
<style targettype="{x:Type Menu}" x:key="{x:Type Menu}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Menu}">
<Border
Background="{StaticResource MenuBackgroundBrush}"
BorderBrush="{StaticResource MenuBackgroundBrush}"
BorderThickness="1">
<StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</style>
<style targettype="{x:Type MenuItem}" x:key="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="Col0" MinWidth="17" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="MenuTextColumnGroup"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
<ColumnDefinition x:Name="Col3" Width="14"/>
</Grid.ColumnDefinitions>

<!-- ContentPresenter to show an Icon if needed -->
<ContentPresenter Grid.Column="0" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>

<!-- Glyph is a checkmark if needed for a checkable menu -->
<Grid Grid.Column="0" Visibility="Hidden" Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center">
<Path x:Name="GlyphPanelpath" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" FlowDirection="LeftToRight"/>
</Grid>

<!-- Content for the menu text etc -->
<ContentPresenter Grid.Column="1"
Margin="{TemplateBinding Padding}"
x:Name="HeaderHost"
RecognizesAccessKey="True"
ContentSource="Header"/>

<!-- Content for the menu IGT -->
<ContentPresenter Grid.Column="2"
Margin="8,1,8,1"
x:Name="IGTHost"
ContentSource="InputGestureText"
VerticalAlignment="Center"/>

<!-- Arrow drawn path which points to the next level of the menu -->
<Grid Grid.Column="3" Margin="4,0,6,0" x:Name="ArrowPanel" VerticalAlignment="Center">
<Path x:Name="ArrowPanelPath" HorizontalAlignment="Right" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,0 L0,8 L4,4 z"/>
</Grid>

<!-- The Popup is the body of the menu which expands down or across depending on the level of the item -->
<Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" x:Name="SubMenuPopup" Focusable="false" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
<Border x:Name="SubMenuBorder" BorderBrush="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}" BorderThickness="1" Padding="2,2,2,2">
<Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True">
<!-- StackPanel holds children of the menu. This is set by IsItemsHost=True -->
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
</Grid>
</Border>
</Popup>
</Grid>
</Border>

<!-- These triggers re-configure the four arrangements of MenuItem to show different levels of menu via Role -->
<ControlTemplate.Triggers>
<!-- Role = TopLevelHeader : this is the root menu item in a menu; the Popup expands down -->
<Trigger Property="Role" Value="TopLevelHeader">
<Setter Property="Padding" Value="6,1,6,1"/>
<Setter Property="Placement" Value="Bottom" TargetName="SubMenuPopup"/>
<Setter Property="MinWidth" Value="0" TargetName="Col0"/>
<Setter Property="Width" Value="Auto" TargetName="Col3"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="GlyphPanel"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="IGTHost"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
</Trigger>

<!-- Role = TopLevelItem : this is a child menu item from the top level without any child items-->
<Trigger Property="Role" Value="TopLevelItem">
<Setter Property="Padding" Value="6,1,6,1"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
</Trigger>

<!-- Role = SubMenuHeader : this is a child menu item which does not have children -->
<Trigger Property="Role" Value="SubmenuHeader">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Padding" Value="0,2,0,2"/>
</Trigger>

<!-- Role = SubMenuItem : this is a child menu item which has children-->
<Trigger Property="Role" Value="SubmenuItem">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="Padding" Value="0,2,0,2"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
</Trigger>
<Trigger Property="IsSuspendingPopupAnimation" Value="true">
<Setter Property="PopupAnimation" Value="None" TargetName="SubMenuPopup"/>
</Trigger>

<!-- If no Icon is present the we collapse the Icon Content -->
<Trigger Property="Icon" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
</Trigger>

<!-- The GlyphPanel contains the CheckMark -->
<Trigger Property="IsChecked" Value="true">
<Setter Property="Visibility" Value="Visible" TargetName="GlyphPanel"/>
<Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
</Trigger>

<!-- Using the system colors for the Menu Highlight and IsEnabled-->
<Trigger Property="IsHighlighted" Value="true">
<Setter Property="Background" Value="{StaticResource HighlightedBackgroundBrush}" TargetName="Border"/>
<Setter Property="Foreground" Value="{StaticResource HighlightedText}"/>
<Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}" TargetName="Border"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="false">
<Setter Property="Background" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/>
<Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="LightGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</style>
</page.resources>
<grid background="#FF252525" x:name="mainGrid">
<menu grid.columnspan="2" verticalalignment="Top" x:name="mainMenu">
<menuitem header="File" x:name="fileMenu"/>
<menuitem header="Open..." x:name="fileOpenMenuItem">
<menuitem header="Save" x:name="fileSaveMenuItem"/>
<menuitem background="{DynamicResource menuItemBrush}" header="Save As..." x:name="fileSaveAsMenuItem"/>
</menuitem>
</menu>
</grid>
</page>

知食

赞同来自:

您无法执行此操作,而无需覆盖菜单项的模板。 模板内部深度是有权的控制实体 "Part_Popup". 您需要更改其背景颜色。

要回复问题请先登录注册