Configuring the context menu in WPF

I have a project here where I need to configure the context menu in my WPF application, in which the button will be placed at the bottom of all menu items.

However, if I added the button via XAML, it would appear as another element in the collection in the context menu, and the mouse-over highlight would act on it.

I would like the context menu to adjust to a mesh style, thanks to which I could customize the style under it.

Any idea how I can achieve this (preferably in XAML)?

+5
source share
3 answers

This is actually quite simple in XAML. Just define it under the element for which you want to have a context menu.

        <Border>
            <Border.ContextMenu>
                <ContextMenu>
                    <ContextMenu.Template>
                        <ControlTemplate>
                            <Grid>
                                <!--Put anything you want in here.-->
                            </Grid>
                        </ControlTemplate>
                    </ContextMenu.Template>
                </ContextMenu>
            </Border.ContextMenu>
        </Border>
+15
source

:

. Header MenuItem, MenuItem, MenuItem.

<ContextMenu>
    <ContextMenu.Items>
       <MenuItem>
          <MenuItem.Header>
             <StackPanel>
                <TextBlock Text="Item 1"/>
                <Button Content="Button 1" Margin="5"/>
             </StackPanel>
          </MenuItem.Header>
        </MenuItem>
        <MenuItem>
          <MenuItem.Header>
             <StackPanel>
                <TextBlock Text="Item 2"/>
                <Button Content="Button 2" Margin="5"/>
              </StackPanel>
           </MenuItem.Header>
          </MenuItem>
     </ContextMenu.Items>
 </ContextMenu>

ContextMenu:

enter image description here

MenuItem Button ..

, !

+4

You can start with an example style / template (from here ) for ContextMenuand customize it to your needs.

<Style TargetType="{x:Type ContextMenu}">
  <Setter Property="SnapsToDevicePixels" Value="True" />
  <Setter Property="OverridesDefaultStyle" Value="True" />
  <Setter Property="Grid.IsSharedSizeScope" Value="true" />
  <Setter Property="HasDropShadow" Value="True" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ContextMenu}">
        <Border x:Name="Border"
                Background="{StaticResource MenuPopupBrush}"
                BorderThickness="1">
          <Border.BorderBrush>
            <SolidColorBrush Color="{StaticResource BorderMediumColor}" />
          </Border.BorderBrush>
          <StackPanel IsItemsHost="True"
                      KeyboardNavigation.DirectionalNavigation="Cycle" />
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="HasDropShadow" Value="true">
            <Setter TargetName="Border" Property="Padding" Value="0,3,0,3" />
            <Setter TargetName="Border" Property="CornerRadius" Value="4" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
+1
source

All Articles