Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a right-click context menu for a button in WPF

i know how to create a left-click context menu for a button, but I am not too sure how to do it for a right click? (i.e. how to specify that the context menu should appear on a right-click, not a left click).

Many thanks.

like image 383
RKM Avatar asked Jul 21 '11 13:07

RKM


People also ask

How to use ContextMenu in WPF?

A ContextMenu is attached to a specific control. The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control, for example, a Button. Users right-click the control to make the menu appear.

What is DataContext in WPF?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

Which actions displays the Windows context menu?

In Microsoft Windows, pressing the Application key or Shift+F10 opens a context menu for the region that has focus.

What is a WPF button?

Introduction to WPF Button using C# and XAML The Button control is one of the basic controls in WPF. A button is used to click and execute code on its click event handler. A button control can be represented at design-time using the XAML <Button> element. The Button class in C# represents the WPF button at run-time.


1 Answers

Here is a sample:

<Button Content="button" Name="btn" Width="100">
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Cut"/>
            <MenuItem Header="Copy"/>
            <MenuItem Header="Paste"/>
        </ContextMenu>
    </Button.ContextMenu>
</Button>

enter image description here

like image 120
Bathineni Avatar answered Oct 04 '22 06:10

Bathineni