菜单项样式

问题描述 投票:0回答:1

我有一个包含以下项目的上下文菜单

    <ContextMenu x:Name="NotificoContextMenu" x:Shared="false" x:Key="SysTrayMenu" x:FieldModifier="public" Loaded="NotificoContextMenu_Loaded" Style="{DynamicResource conte}">
        <MenuItem Header="Connect" x:Name="ConnectMenuItem" x:FieldModifier="public" />
        <MenuItem Header="Ping" Command="{Binding PingCommand}"/>
        <MenuItem Header="Show Window" Command="{Binding ShowWindowCommand}" />
        <Separator />
        <MenuItem Header="Exit" Command="{Binding ExitApplicationCommand}" />
    </ContextMenu>

对于 ConnectMenuItem,我从代码中添加菜单项

我有一个上下文菜单样式,它适用于所有菜单项,除了我从代码中添加的菜单项,我应该做什么


这是我的上下文菜单的样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type ContextMenu}" x:Key="conte">
        <Setter Property="Background" Value="{DynamicResource PrimaryDark}" />
        <Setter Property="Foreground" Value="{DynamicResource Secendery}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContextMenu}">
                    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}" BorderThickness="0.5" CornerRadius="5">
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow" Value="true">
                            <Setter TargetName="Border" Property="Padding" Value="0,3,0,3" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
c# wpf xaml contextmenu menuitem
1个回答
0
投票

日夜寻找时发现了一些灵魂出处 这是完整的 Windows 资源文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type ContextMenu}" x:Key="conte">
        <Setter Property="Background" Value="{DynamicResource PrimaryDark}" />
        <Setter Property="Foreground" Value="{DynamicResource Secendery}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="HasDropShadow" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContextMenu}">
                    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}" BorderThickness="0.85" CornerRadius="5">
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle">
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style 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="10,0,0,0" Visibility="Collapsed" VerticalAlignment="Center"/>
                            <ContentPresenter x:Name="content" ContentSource="Header" Margin="10,3" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </DockPanel>
                        <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="false" HorizontalOffset="0" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Left" VerticalOffset="0">
                            <Border BorderThickness="0.85" CornerRadius="2" BorderBrush="{DynamicResource Secendery}" Background="{TemplateBinding Background}">
                                <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="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                                    </Grid>
                                </ScrollViewer>
                            </Border>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="TextBlock.Foreground" Value="White" TargetName="content"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</ResourceDictionary>
© www.soinside.com 2019 - 2024. All rights reserved.