Customize the appearance style of ContextMenu




<!--Customize the appearance style of ContextMenu-->
        <Style x:Key="ContextMenyStyle" TargetType="{x:Type ContextMenu}">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
            <Setter Property="Focusable" Value="false" />
            <Setter Property="HasDropShadow" Value="true"/>
            <Setter Property="BorderThickness" Value="{TemplateBinding BorderThickness}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                        <Border Name="Border" Background="{DynamicResource BorderBrush}" BorderBrush="{DynamicResource NormalBorderBrush}" BorderThickness="1" >
                            <StackPanel IsItemsHost="true" KeyboardNavigation.DirectionalNavigation="Cycle" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasDropShadow" Value="true">
                                <Setter TargetName="Border" Property="Padding" Value="0" />
                                <Setter TargetName="Border" Property="CornerRadius" Value="0" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
 <!--Customize the style of the separator between menu items-->
        <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
            <Setter Property="Height" Value="1" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Separator}">
                        <Border BorderBrush="#888888" BorderThickness="2" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!--TopLevelHeader-->
        <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border Name="Border">
                <Grid>
                    <ContentPresenter Margin="6 3 6 3" ContentSource="Header" RecognizesAccessKey="True">
                        <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="true" Focusable="false" PopupAnimation="Slide" >
                            <Border Name="SubmenuBorder" SnapsToDevicePixels="true" Background="#FFFFFF" BorderBrush="#888888" BorderThickness="1">
                                <StackPanel IsItemsHost="true" KeyboardNavigation.DirectionalNavigation="Cycle" />
                            </Border>
                        </Popup>
                    </ContentPresenter>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                    <Setter TargetName="Popup" Property="PopupAnimation" Value="none" />
                </Trigger>
                <Trigger Property="IsHighlighted" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="Green"/>
                    <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
                </Trigger>
                <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                    <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
                    <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="#888888" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <!--TopLevelItem-->
        <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border Name="Border">
                <Grid>
                    <ContentPresenter Margin="20,3,6,10" ContentSource="Header" RecognizesAccessKey="true">

                    </ContentPresenter>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsHighlighted" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="blue"/>
                    <Setter TargetName="Border" Property="BorderBrush" Value="Transparent" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="#888888" />
                </Trigger>
            </ControlTemplate.Triggers>

        </ControlTemplate>

        <!-- SubmenuItem Customize the appearance style of menu items -->
        <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border Name="Border">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
                        <ColumnDefinition Width="13"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Name="Icon" Margin="6,2,6,2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ContentSource="Icon"/>
                    <Border Name="Check" Width="13" Height="13" Visibility="Hidden" Margin="6,0,6,0" Background="#C0C0C0\ " BorderThickness="1" BorderBrush="#404040">
                        <Path Name="CheckMark" Width="7" Height="7" Visibility="Visible" SnapsToDevicePixels="False" Stroke="#404040" StrokeThickness="1 " Data="M 0 0 L 7 7 M 0 7 L 7 0" />
                    </Border>
                    <ContentPresenter Name="HeaderHost" Grid.Column="1" ContentSource="Header" RecognizesAccessKey="true" />
                    <TextBlock x:Name="InputGestureText" Grid.Column="2" Background="Green" Text="{TemplateBinding InputGestureText}" Margin="5 2 0 2" DockPanel.Dock ="Right" />
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
                </Trigger>
                <Trigger Property="IsChecked" Value="true">
                    <Setter TargetName="CheckMark" Property="Visibility" Value="Visible" />
                </Trigger>
                <Trigger Property="IsCheckable" Value="true">
                    <Setter TargetName="Check" Property="Visibility" Value="Hidden"/>
                    <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
                </Trigger>
                <!--Trigger setting of highlight color when menu item is selected-->
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="green" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="White" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <!--SubmenuItem custom menu item appearance style-->

            <!--SubMenuHeader-->
        <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border Name="Border">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" SharedSizeGroup="Icon" />
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto" SharedSizeGroup="Shortcut"/>
                        <ColumnDefinition Width="13" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Name="Icon" Margin="6 0 6 0 " HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ContentSource="Icon"/>
                    <ContentPresenter Name="HeaderHost" Grid.Column="1" ContentSource="Header" RecognizesAccessKey="true" />
                    <TextBlock Name="InputGestureText" Grid.Column="2" Text="{TemplateBinding InputGestureText}" Margin="5 2 2 2" DockPanel.Dock="Right" />
                    <Path Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M0 0 L 0 7 L 4 3.5 Z" />
                    <Popup Name="Popup" Placement="Right" HorizontalOffset="-4" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="true" Focusable="false" PopupAnimation= "Slide">
                        <Border Name="SubmenuBorder" SnapsToDevicePixels="true" Background="#FFFFFF" BorderBrush="#888888" BorderThickness="1">
                            <StackPanel IsItemsHost="true" KeyboardNavigation.DirectionalNavigation="Cycle" />
                        </Border>
                    </Popup>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
                </Trigger>
                <Trigger Property="IsHighlighted" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                </Trigger>
                <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                    <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4" />
                    <Setter TargetName="SubmenuBorder" Property="Padding" Value="0 3 0 3" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="#888888" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
 
        <!--MenuItem Style-->
        <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}" >
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Style.Triggers>
                <Trigger Property="Role" Value="TopLevelHeader">
                    <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}" />
                    <Setter Property="Grid.IsSharedSizeScope" Value="true" />
                </Trigger>
                <Trigger Property="Role" Value="TopLevelItem">
                    <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}" />
                </Trigger>
                <Trigger Property="Role" Value="SubmenuHeader">
                    <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}" />
                </Trigger>
                <Trigger Property="Role" Value="SubmenuItem">
                    <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}" />
                </Trigger>
            </Style.Triggers>
        </Style>