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
control.ContextMenu.IsOpen = true, this require control to be at least FrameworkElement though.control.ContextMenureturns null. But when I right click on my control manually, ContextMenu works just fine