SciChart¬ģ the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy

We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

1
0

Hi Scichart,

I am struggling with a ContextMenu on a SciChartSurface that wont be displayed.

The problem started when i added mouse panning with the right mouse-button which is the same button the ContextMenu is activated on.

So will this combination even work – with both a ContextMenu and mouse panning with the right mouse button?

To manually validate if the ContextMenu should open or if the panning should be enabled, I have tried to get the mouse coordinates manually in C# to see if the mouse was moved(panning) or if it was a click(ContextMenu). This did not work very well when i afterwards started to pan/zoom again.

Do you have a code example using both a ContextMenu and mouse panning using right mouse button?

Best regards,
Jeppe

  • You must to post comments
2
0

Update: SciChart v4

We have a working solution with SciChart v4 below. What we have done is implemented showing the context menu on SciChartSurface.PreviewMouseUp event.

We have also overridden ZoomPanModifier to ensure that e.Handled is only ever true when panning (e.g. if Start / End Point are not the same).

Here’s the code:

MainWindow.xaml

<Window x:Class="WpfApplication85.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
        xmlns:wpfApplication85="clr-namespace:WpfApplication85"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <s:SciChartSurface Name="sciChart" PreviewMouseUp="SciChart_OnPreviewMouseUp">
            <s:SciChartSurface.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Item1" />
                    <MenuItem Header="Item2" />
                </ContextMenu>
            </s:SciChartSurface.ContextMenu>

            <s:SciChartSurface.XAxis>
                <s:NumericAxis/>
            </s:SciChartSurface.XAxis>

            <s:SciChartSurface.YAxis>
                <s:NumericAxis/>
            </s:SciChartSurface.YAxis>

            <s:SciChartSurface.ChartModifier>
                <s:ModifierGroup>
                    <wpfApplication85:ZoomPanModifierEx ExecuteOn="MouseRightButton" ClipModeX="None"></wpfApplication85:ZoomPanModifierEx>                    
                    <s:CursorModifier ReceiveHandledEvents="True" />
                </s:ModifierGroup>
            </s:SciChartSurface.ChartModifier>
        </s:SciChartSurface>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void SciChart_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (e.RightButton == MouseButtonState.Pressed)
        {
            ((SciChartSurface)sender).ContextMenu.IsOpen = true;
            e.Handled = true;
        }
    }
}  

ZoomPanModifierEx.cs

public class ZoomPanModifierEx : ZoomPanModifier
{
    private Point _startPoint;

    public override void OnModifierMouseDown(ModifierMouseArgs e)
    {
        base.OnModifierMouseDown(e);

        _startPoint = e.MousePoint;
    }

    public override void OnModifierMouseUp(ModifierMouseArgs e)
    {
        base.OnModifierMouseUp(e);

        if (_startPoint == e.MousePoint) e.Handled = false;
    }
}

Best regards,
Andrew

  • Andrew
    EDIT: You don’t actually need the explicit PreviewMouseUp event either. It works without it. The important point is to ensure that MouseUp is not handled in modifiers if you don’t complete a pan operation.
  • kurtisnet
    This works perfectly! Thank you Andrew for the code. :)
  • You must to post comments
0
0

Hi Jeppe,

This is because the ZoomPanModifier menu sets e.Handled = true on the mouse event when it handles a pan, which will stop WPF raising the event to other parts of your UI (such as Context Menu).

There is no simple fix for this. If you were to override ZoomPanModifier.OnModifierMouseDown and set e.Handled = true, the pan would work, but the context menu would show as well (e.g. you would get a floating menu while the chart is panning).

Your best bet is to create a custom pan modifier based off our tutorial Custom ChartModifiers – Part 2 – Custom ZoomPanModifier and Zooming on KeyPress and use this modifier to handle both showing of context menu and panning.

Best regards,
Andrew

  • You must to post comments
0
0

Hi Andrew,

Sounds good, i’ll take a look at the custom chartmodifiers. Thanks for the suggestion! ūüôā

Best regards,
Jeppe

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.