Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Context Menu in WPF

I have a project here where I require customizing the context menu in my WPF application in which a button would be placed at the bottom of all the menuitems.

However, if I were to add the button through the XAML, it would appear as another item in the collection in the context menu and the mouse-over highlight would act on it.

I would like to have the context menu tuned to a grid-like style whereby I could customize the style underneath it.

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

like image 788
Alan Ho Avatar asked Mar 28 '13 06:03

Alan Ho


3 Answers

It's actually pretty straightforward in the XAML. Just define it under the element for which you want to have the context menu.

        <Border>
            <Border.ContextMenu>
                <ContextMenu>
                    <ContextMenu.Template>
                        <ControlTemplate>
                            <Grid>
                                <!--Put anything you want in here.-->
                            </Grid>
                        </ControlTemplate>
                    </ContextMenu.Template>
                </ContextMenu>
            </Border.ContextMenu>
        </Border>
like image 193
Ben Wilde Avatar answered Nov 13 '22 22:11

Ben Wilde


For your menu item style with the button in the item you can use the following code:

Note - Adding items to the Header will keep it in the same MenuItem, but if added to the MenuItem only it will be regarded as a new 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>

This will be the resulting ContextMenu:

enter image description here

From there you can style the MenuItem or Button etc.

Hope it helps!

like image 28
Chrisjan Lodewyks Avatar answered Nov 13 '22 21:11

Chrisjan Lodewyks


You can start with the example style/template (from here) for ContextMenu and adjust 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>
like image 4
Drew Noakes Avatar answered Nov 13 '22 20:11

Drew Noakes