UIElement.ContextRequested Event

Definition

Occurs when the user has completed a context input gesture, such as a right-click.

// Register
event_token ContextRequested(TypedEventHandler<UIElement, ContextRequestedEventArgs const&> const& handler) const;

// Revoke with event_token
void ContextRequested(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::ContextRequested_revoker ContextRequested(auto_revoke_t, TypedEventHandler<UIElement, ContextRequestedEventArgs const&> const& handler) const;
public event TypedEventHandler<UIElement,ContextRequestedEventArgs> ContextRequested;
function onContextRequested(eventArgs) { /* Your code */ }
uIElement.addEventListener("contextrequested", onContextRequested);
uIElement.removeEventListener("contextrequested", onContextRequested);
- or -
uIElement.oncontextrequested = onContextRequested;
Public Custom Event ContextRequested As TypedEventHandler(Of UIElement, ContextRequestedEventArgs) 
<uiElement ContextRequested="eventhandler"/>

Event Type

Windows requirements

Device family
Windows 10 Anniversary Edition (introduced in 10.0.14393.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v3.0)

Examples

This example shows how to show and hide a context menu when the user right-clicks or performs an equivalent action. The context menu offers the options Red and Green and is placed on a rectangle.

A context menu showing the options red and green
<Page
    ...>
    <Page.Resources>
        <MenuFlyout x:Key="colorMenuFlyout">
            <MenuFlyoutItem Text="Red" Tag="red" Click="MenuFlyoutItem_Click"/>
            <MenuFlyoutItem Text="Green" Tag="green" Click="MenuFlyoutItem_Click"/>
        </MenuFlyout>
    </Page.Resources>

    <Grid>
        <Rectangle Width="100" Height="100" Fill="Yellow"
                   ContextRequested="Color_ContextRequested" 
                   ContextCanceled="Color_ContextCanceled">
        </Rectangle>
    </Grid>
</Page>
public sealed partial class MainPage : Page
{
    MenuFlyout colorMenuFlyout;

    public MainPage()
    {
        this.InitializeComponent();

        colorMenuFlyout = Resources["colorMenuFlyout"] as MenuFlyout;
    }

    private void Color_ContextRequested(UIElement sender, ContextRequestedEventArgs args)
    {
        Point point = new Point(0,0);

        if (args.TryGetPosition(sender, out point))
        {
            colorMenuFlyout.ShowAt(sender, point);
        }
        else
        {
            colorMenuFlyout.ShowAt((FrameworkElement)sender);
        }
    }

    private void Color_ContextCanceled(UIElement sender, RoutedEventArgs args)
    {
        colorMenuFlyout.Hide();
    }

    private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
    {
        var item = sender as MenuFlyoutItem;
        var target = colorMenuFlyout.Target;
        if (string.Equals(item.Tag.ToString(), "red"))
        {
            ((Rectangle)target).Fill = new SolidColorBrush(Windows.UI.Colors.Red);
        }
        else if (string.Equals(item.Tag.ToString(), "green"))
        {
            ((Rectangle)target).Fill = new SolidColorBrush(Windows.UI.Colors.Green);
        }
    }
}

Remarks

We recommend the you set the ContextFlyout property to add a context menu to an element. When ContextFlyout is set, the context menu is shown and hidden automatically, and this event is marked as handled. You should only handle ContextRequested and ContextCanceled if you do not set ContextFlyout.

If you handle this event to show the context flyout, you should also handle the ContextCanceled event to hide the flyout if the request is canceled.

ContextRequested is a routed event. For more info on the routed event concept, see Events and routed events overview.

Applies to

See also