0

I am trying to emulate right click from the code behind like so:

static void RightMouseClick(Visual visual)
{
    UIElement uel = (UIElement)visual;
    var e = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Right);
    e.RoutedEvent = Mouse.MouseDownEvent;
    uel.RaiseEvent(e);
    
    Thread.Sleep(100);
    
    e.RoutedEvent = Mouse.MouseUpEvent;
    uel.RaiseEvent(e);
}

I expect for ContextMenu to pop up on my control, but this doesn't happen. Seems like the right click is working, but ContextMenu doesn't appear.

Edit: This problem exists specifically with ScrollBar, I need to access its default ContextMenu, that is, I can't create my own ContextMenu.

3
  • AFAIK, you can open context menu control.ContextMenu.IsOpen = true, this require control to be at least FrameworkElement though. Commented Sep 5, 2024 at 15:07
  • @Sinatr I tried than,control.ContextMenu returns null. But when I right click on my control manually, ContextMenu works just fine Commented Sep 5, 2024 at 15:10
  • I suggest you to prepare minimal reproducible example, then someone may try it out and find the problem. Commented Sep 5, 2024 at 15:15

1 Answer 1

0

I have succeeded by initializing my own context menu and opening it. For example I execute this code on my Grid initialization:

private void Grid_Initialized(object sender, EventArgs e)
{
    var contextMenu = new ContextMenu(); 
    var mi = new MenuItem();
    mi.Header = "File";
    var mia = new MenuItem();
    mia.Header = "New";
    mi.Items.Add(mia);
    var mib = new MenuItem();
    mib.Header = "Open";
    mi.Items.Add(mib);
    var mib1 = new MenuItem();
    mib1.Header = "Recently Opened";
    mib.Items.Add(mib1);
    var mib1a = new MenuItem();
    mib1a.Header = "Text.xaml";
    mib1.Items.Add(mib1a);
    contextMenu.Items.Add(mi);
    contextMenu.IsOpen = true;

    var ctrl = (FrameworkElement)sender;
    ctrl.ContextMenu = contextMenu;
}

Here's visual example: enter image description here

Update

If you want context menu similair to what you'd have when right clicking scrollbar, you would need to use ScrollViewer and it's methods.

Here's sample XAML:

<Window
    x:Class="test_projects.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:test_projects"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    MouseRightButtonUp="Window_MouseRightButtonUp"
    mc:Ignorable="d">
    <ScrollViewer x:Name="ScrollViewer">
        <StackPanel>
            <TextBlock Margin="100" Text="blabla" />
            <TextBlock Margin="100" Text="blabla" />
            <TextBlock Margin="100" Text="blabla" />
        </StackPanel>
    </ScrollViewer>
</Window>

And in code behing you still have to create own context menu, but this time you can leverage ScrollViewers method to perform scrolling (this is what you would get when right clicking a scrollbar as well - just different scroll options). Here's sample code behind:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace test_projects;


// Just helper enum
public enum ScrollingOptions
{
    Up, Down, Top, Bottom,
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        ContextMenu contextMenu = new ContextMenu();
        contextMenu.Items.Add(CreateMenuItem(ScrollingOptions.Up));
        contextMenu.Items.Add(CreateMenuItem(ScrollingOptions.Down));
        contextMenu.Items.Add(new Separator());
        contextMenu.Items.Add(CreateMenuItem(ScrollingOptions.Top));
        contextMenu.Items.Add(CreateMenuItem(ScrollingOptions.Bottom));
        contextMenu.IsOpen = true;

        ContextMenu = contextMenu;
    }

    private MenuItem CreateMenuItem(ScrollingOptions scrollingOptions)
    {
        MenuItem menuItem = new MenuItem();
        switch (scrollingOptions)
        {
            case ScrollingOptions.Top:
                menuItem.Header = "Top";
                menuItem.Click += (s, e) => ScrollViewer.ScrollToTop();
                break;

            case ScrollingOptions.Bottom:
                menuItem.Header = "Bottom";
                menuItem.Click += (s, e) => ScrollViewer.ScrollToBottom();
                break;

            case ScrollingOptions.Up:
                menuItem.Header = "Up";
                menuItem.Click += (s, e) => ScrollViewer.LineUp();
                break;

            case ScrollingOptions.Down:
                menuItem.Header = "Down";
                menuItem.Click += (s, e) => ScrollViewer.LineDown();
                break;
        };

        return menuItem;
    }
}

Reference: How to: Use the Content-Scrolling Methods of ScrollViewer

Sign up to request clarification or add additional context in comments.

4 Comments

This might help, but my goal is to access default ContextMenu, not to create my own. Thank you anyway
@kuzyun Wht is the default context menu? As when clicking on app, no context menu shows. If you are thinking about context menu like showing when right clicking on the desktop, it is completely different thing - how would you want "file specific" operations to work in your app?
I have this problem with scrollBar, and when I right click on it, the menu appears (Scroll Here, Top, Bottom, etc.). This kind of menu I'm trying to access
@kuzyun Please see, what I could find out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.