SciChartĀ® the market leader in Fast WPF Charts, WPF 3D Charts, and iOS Chart & 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!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

1
0

Hello…

It is me again….

As I have said before, our application is using MVVM and WPF (C#) and I am wondering if you guys have an example or if you could give me some suggestion of how to implement a menu when ALT-right-click on the chart, like in the picture attached?

Hope you can help me…

Thanks very much.. šŸ™‚

Images
  • You must to post comments
0
0

Hi there,

Unfortunately, we don’t have such an example, but lots of SciChart customers have implemented this behavior for their apps, so I think there shouldn’t be any complications. There is an example how to do this using custom modifier:

        <s:SciChartSurface Name="sciChart"
                           s:ThemeManager.Theme="BlackSteel">
            <s:SciChartSurface.ContextMenu>
                <ContextMenu IsOpen="{Binding IsOpen, Mode=OneWay, ElementName=ContextMenuModifier}">
                    <MenuItem Header="Item1" />
                    <MenuItem Header="Item2" />
                </ContextMenu>
            </s:SciChartSurface.ContextMenu>

...

            <s:SciChartSurface.ChartModifier>
                <s:ModifierGroup>
                    <CreateSimpleChart:ContextMenuModifier x:Name="ContextMenuModifier" ReceiveHandledEvents="True" />
                    <s:CursorModifier ReceiveHandledEvents="True" />
                </s:ModifierGroup>
            </s:SciChartSurface.ChartModifier>

..and modifier’s code:

    public class ContextMenuModifier: ChartModifierBase
    {
        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool), typeof(ContextMenuModifier), new PropertyMetadata(false));

        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }

        public override void OnAttached()
        {
            base.OnAttached();

            // Subscribe to listen to ContextMenuOpening event on ParentSurface
            (ParentSurface as SciChartSurface).ContextMenuOpening += OnSurfaceContextMenuOpening;
        }

        private void OnSurfaceContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            // If !IsOpen, prevent menu from being show by handling the event
            e.Handled = !IsOpen;
        }

        public override void OnModifierMouseDown(ModifierMouseArgs e)
        {
            IsOpen = false;
        }

        public override void OnModifierMouseUp(ModifierMouseArgs e)
        {
            // Open menu if right mouse btn was clicked and Alt btn was pressed
            // IsOpen should be OneWay-bound to SciChartSurface.ContextMenu.IsOpen
            IsOpen = (e.MouseButtons == MouseButtons.Right) && (e.Modifier == MouseModifier.Alt);
        }
    }

I think the above give you an idea how to implement similar behavior. Please, notice, that you need to release Alt button quickly, because the ContextMenu will disappear just after being shown(strange issue).

Also there are a few threads connected to this issue, you can find some useful info or another ideas there: ContextMenu for chart series and Keyboard shortcuts to pan.

Hope this helps!

Best regards,
Yuriy

  • vaquita50
    Thanks Yuriy... This is a great start.... I will try to implement it and if I have any problem I may bother you again. Have a nice weekend!! :)
  • Andrew
    I must say this is good stuff - might make a good example (or tutorial). Thanks Yuriy!
  • You must to post comments
0
0

I am using this code example but have trouble connecting the MenuItems to a command.

I am using MVVM Light and created a RelayCommand on ContextMenuModifier and used the same name in Command, but the attached subroutine never fires ( as in other parts of my code). How and to where do I bind the Command of a ContextMenuItem as in this example.

New to WPF. Love the binding, but hate the debugging to see to what it is actually bound too šŸ™

  • Andrew
    I think you need to check the DataContext property of the menu that is shown. Any commands in a ViewModel must be bound to MenuItems, the MenuItem needs to have the DataContext set to the instance of the view model. If it's not, then you can wire it up yourself, e.g. by setting Menu.DataContext = ChartModifierBase.DataContext (assuming, that the data context of your chart modifier base is in fact the correct view model). PS Finally, WPF Snoop (free from codeplex) can help you debug MVVM bindings and show binding errors. Best regards, Andrew
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.