我们可以自定义 MenuItem 的模板,使用触发器来改变 TextBlock 的颜色。具体实现代码如下:
<Window.Resources>
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Gray"/>
<ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
<Border x:Name="Border" BorderThickness="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="True">
<Grid>
<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" Margin="4,0,6,0" VerticalAlignment="Center" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality"/>
<TextBlock x:Name="Text" Text="{TemplateBinding Header}" Margin="4,0,6,0" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}"/>
<TextBlock x:Name="InputGestureText" Text="{TemplateBinding InputGestureText}" Margin="0,0,6,0" VerticalAlignment="Center" Visibility="Collapsed" Foreground="{TemplateBinding Foreground}"/>
<TextBlock x:Name="Text2" Text="{TemplateBinding Header}" Margin="4,0,6,0" VerticalAlignment="Center" Foreground="{StaticResource DisabledForegroundBrush}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}}, Path=IsEnabled}" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation" Value="True">
<Setter TargetName="Popup" Property="PopupAnimation" Value="None"/>
</Trigger>
<Trigger Property="Icon" Value="{x:Null}">
<Setter TargetName="Icon" Property="Visibility" Value