Search code examples
c#wpfxamlcontextmenumenuitem

Menuitem item style


I have a context menu with the following items

    <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>

For the ConnectMenuItem im adding menuitems in it from code

I have an style for the contextmenu it applys to all the menu items exept the ones i add from code what should I do


This is the style for my context menu

<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>

Solution

  • found some souloution while searching day and night for it here is the full windows resourses file :

    <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>