Language

UIElement.ContextRequested Event

Definition

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

// Register
event_token ContextRequested(TypedEventHandler 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 const& handler) const;
public event TypedEventHandler 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) 

Event Type

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

    
        
            
            
        
    

    
        
        
    

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)
    {
        var 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