Customizing Context Menu in WPF

contextmenu, wpf, xaml

Solution

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>

Problem

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

Original source