ContextMenu

De Banane Atomic
Aller à la navigationAller à la recherche

ContextMenu

Xaml.svg
<UserControl.ContextMenu>
    <ContextMenu>
        <MenuItem Header="Choix 1"
                  InputGestureText="Ctrl+1"
                  Click="MenuItem_Click">
        </MenuItem>

        <MenuItem Header="Choix 2">
            <MenuItem Header="Sous-Choix 2-1"
                        InputGestureText="Ctrl+2"
                        Click="MenuItem_Click">
            </MenuItem>
        </MenuItem>

Contrôler l'ouverture du menu

Xaml.svg
<UserControl ContextMenuOpening="UserControl_ContextMenuOpening">
Csharp.svg
private void UserControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    // désactive l'affichage du Menu sur clique-droit
    e.Handled = true;
}

Ouvrir le menu depuis le code

Csharp.svg
ContextMenu.PlacementTarget = this;
ContextMenu.IsOpen = true;

Style

Xaml.svg
<!-- Modifie les items texte blanc sur fond noir -->
<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Background"
            Value="{StaticResource ResourceKey=mainBackgroundBrush}"/>
    <Setter Property="Foreground"
            Value="White"/>
</Style>

<Style TargetType="{x:Type ContextMenu}">
    <!-- Changement de la couleur de fond du Menu -->
    <Setter Property="Background"
            Value="Black" />
    <!-- Décalage pour ne pas afficher un petit bout blanc -->
    <Setter Property="Padding"
            Value="-1" />
</Style>

Le rectangle bleu transparent sur le Highlighted item ne peut être stylé qu'en redéfinissant le Template de MenuItem.

Xaml.svg
<!-- Code du ControlTemplate de base de MenuItem modifié pour afficher un rectangle orange sur l'élément Highlighted -->
<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="MenuItem"
                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                             xmlns:s="clr-namespace:System;assembly=mscorlib">
                <Grid SnapsToDevicePixels="True">
                    <Rectangle RadiusX="2"
                               RadiusY="2"
                               Fill="{TemplateBinding Panel.Background}"
                               Stroke="{TemplateBinding Border.BorderBrush}"
                               StrokeThickness="1"
                               Name="Bg" />
                    <Rectangle RadiusX="2"
                               RadiusY="2"
                               Name="InnerBorder"
                               Margin="1,1,1,1" />
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"
                                              MinWidth="24"
                                              SharedSizeGroup="MenuItemIconColumnGroup" />
                            <ColumnDefinition Width="4" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="37" />
                            <ColumnDefinition Width="Auto"
                                              SharedSizeGroup="MenuItemIGTColumnGroup" />
                            <ColumnDefinition Width="17" />
                        </Grid.ColumnDefinitions>

                        <ContentPresenter Content="{TemplateBinding MenuItem.Icon}"
                                          ContentSource="Icon"
                                          Name="Icon"
                                          Margin="1,1,1,1"
                                          VerticalAlignment="Center"
                                          SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />

                        <Border BorderThickness="1,1,1,1"
                                CornerRadius="3,3,3,3"
                                BorderBrush="#FFCDD3E6"
                                Background="#FFE6EFF4"
                                Name="GlyphPanel"
                                Width="22"
                                Height="22"
                                Margin="1,1,1,1"
                                Visibility="Hidden">
                            <Path Data="M0,5.1L1.7,5.2 3.4,7.1 8,0.4 9.2,0 3.3,10.8z"
                                  Fill="#FF0C12A1"
                                  Name="Glyph"
                                  Width="9"
                                  Height="11"
                                  FlowDirection="LeftToRight" />
                        </Border>

                        <ContentPresenter RecognizesAccessKey="True"
                                          Content="{TemplateBinding HeaderedContentControl.Header}"
                                          ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                          ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                                          ContentSource="Header"
                                          Margin="{TemplateBinding Control.Padding}"
                                          SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                          Grid.Column="2" />

                        <TextBlock Text="{TemplateBinding MenuItem.InputGestureText}"
                                   Margin="{TemplateBinding Control.Padding}"
                                   Grid.Column="4" />
                    </Grid>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="MenuItem.Icon">
                        <Setter Property="UIElement.Visibility"
                                TargetName="Icon">
                            <Setter.Value>
                                <x:Static Member="Visibility.Collapsed" />
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <x:Null />
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="MenuItem.IsChecked">
                        <Setter Property="UIElement.Visibility"
                                TargetName="GlyphPanel">
                            <Setter.Value>
                                <x:Static Member="Visibility.Visible" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="UIElement.Visibility"
                                TargetName="Icon">
                            <Setter.Value>
                                <x:Static Member="Visibility.Collapsed" />
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="MenuItem.IsHighlighted">
                        <Setter Property="Shape.Fill"
                                TargetName="Bg">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0,0"
                                                     EndPoint="0,1">
                                    <LinearGradientBrush.GradientStops>
                                        <!-- Changement des couleurs -->
                                        <GradientStop Color="#FFce9c8b"
                                                      Offset="0" />
                                        <GradientStop Color="#FFdca08b"
                                                      Offset="1" />
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Shape.Stroke"
                                TargetName="Bg">
                            <Setter.Value>
                                <!-- Changement des couleurs -->
                                <SolidColorBrush>#FFc1795e</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Shape.Stroke"
                                TargetName="InnerBorder">
                            <Setter.Value>
                                <!-- Changement des couleurs -->
                                <SolidColorBrush>#FFb78f82</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="UIElement.IsEnabled">
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <SolidColorBrush>#FF9A9A9A</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Panel.Background"
                                TargetName="GlyphPanel">
                            <Setter.Value>
                                <SolidColorBrush>#FFEEE9E9</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Border.BorderBrush"
                                TargetName="GlyphPanel">
                            <Setter.Value>
                                <SolidColorBrush>#FFDBD6D6</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Shape.Fill"
                                TargetName="Glyph">
                            <Setter.Value>
                                <SolidColorBrush>#FF848589</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>