WPF ScottPlot5禁用右键菜单

ScottPlot5右键菜单如何禁用?

探索

我下载了ScottPlot的git仓库源代码,通过在,ScottPlot.WPF.WpfPlotMenu类中的ShowContextMenu方法中下断点,然后右键图表,命中断点时,发现方法的调用堆栈如下:(发生顺序要从下往上看)

ScottPlot.WPF.dll!ScottPlot.WPF.WpfPlotMenu.ShowContextMenu(ScottPlot.Pixel pixel) 行 80	C#
ScottPlot.WPF.dll!ScottPlot.WPF.WpfPlotBase.ShowContextMenu(ScottPlot.Pixel position) 行 53	C#
ScottPlot.dll!ScottPlot.Interactivity.UserActionResponses.SingleClickContextMenu.LaunchContextMenu(ScottPlot.IPlotControl plotControl, ScottPlot.Pixel pixel) 行 11	C#
ScottPlot.dll!ScottPlot.Interactivity.UserActionResponses.SingleClickResponse.Execute(ScottPlot.IPlotControl plotControl, ScottPlot.Interactivity.IUserAction userAction, ScottPlot.Interactivity.KeyboardState keys) 行 55	C#
ScottPlot.dll!ScottPlot.Interactivity.UserInputProcessor.ExecuteUserInputResponses(ScottPlot.Interactivity.IUserAction userAction) 行 230	C#
ScottPlot.dll!ScottPlot.Interactivity.UserInputProcessor.Process(ScottPlot.Interactivity.IUserAction userAction) 行 172	C#
ScottPlot.WPF.dll!ScottPlot.WPF.WpfPlotExtensions.ProcessMouseUp(ScottPlot.Interactivity.UserInputProcessor processor, System.Windows.FrameworkElement fe, System.Windows.Input.MouseButtonEventArgs e) 行 50	C#
ScottPlot.WPF.dll!ScottPlot.WPF.WpfPlotBase.SKElement_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) 行 65	C#

其中ScottPlot.WPF.dll!ScottPlot.WPF.WpfPlotBase.ShowContextMenu(ScottPlot.Pixel position) 方法的源代码为:

public void ShowContextMenu(Pixel position)
{
    Menu?.ShowContextMenu(position);
}

我观察到,ScottPlot.WPF.WpfPlotBase中维护了Menu成员,于是我想到了方法:

解决方案:将WpfPlot的Menu置为Null

以源代码仓库中的沙盒示例代码为例

namespace Sandbox.WPF;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        WpfPlot1.Menu = null;//添加此行可禁用右键菜单
        WpfPlot1.Plot.Add.Signal(Generate.Sin());
        WpfPlot1.Plot.Add.Signal(Generate.Cos());
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WpfPlot1.Reset();
        WpfPlot1.Plot.Add.Signal(Generate.RandomWalk(100));
        WpfPlot1.Refresh();
    }
}

好了,完事儿。

posted @ 2026-02-06 14:57  BigBosscyb  阅读(9)  评论(0)    收藏  举报