ContextMenu doesn’t open when I use right click from code behind

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

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:

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

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật