Blog

ContextMenus in 3.0

Those of you who've upgraded to 3.0 may have noticed a change in the way ContextMenus work.  Since the new Behaviour API now subscribes to right mouse button clicks. those events are now considered to be handled and so any ContextMenus that may have been set don't open. 

The way to get ContextMenus working in 3.0 is to write a Behaviour that will open the menu when the right button is clicked.  We've included an example of a behaviour that will do this - it's really simple and the main methods in a WPF version of such a class are given below:

        public override void PointerPressed(IBehaviourEventSource sender, PointerEventContext context)
        {
            if (Menu != null)
            {
                sender.BehaviourCanvas.CaptureMouse();
                Menu.PlacementTarget = BehaviourCanvas;
                Menu.IsOpen = true;
            }
        }

        public override void PointerReleased(IBehaviourEventSource sender, PointerEventContext context)
        {
            if (Menu != null)
            {
                Menu.IsOpen = false;
                sender.BehaviourCanvas.ReleaseMouseCapture();
            }
        }

        protected override void Init()
        {
            BehaviourCanvas.ContextMenu = Menu;
        }

        public override void DeInit()
        {
            if (Menu != null)
            {
                Menu.IsOpen = false;
            }
        }

The only other piece of the puzzle is to make sure that this new ContextMenuBehaviour is only activated by right mouse button.  You can do that via the new BehaviourActivator property on our behaviours.  Just set that to "RightMouse" in XAML or attache a PointerButtonBehaviourActivator in code behind.

And there it is, ContextMenu support for Visiblox Charts 3.0. 

You can download source code for an example WPF application here - you'll need to drop in a Visiblox Charts dll, either purchased or from your trial to be able to compile it.  Please note this approach requires Visiblox Charts 3.0.1 or later to work correctly.

Comments

Good day How will I be able to achieve this with Silverlight 5? BehaviourCanvas does not have a property for ContextMenu and ContextMenu does not have a property for PlacementTarget. Regards,
 
Posted by Lategan Botha
Hi Lagetan, Silverlight doesn't support context menus out of the box so you'd need to use something like the Silverlight Toolkit (http://silverlight.codeplex.com/) or open your own child window. Either way, the process of attaching it to Visiblox should be very similar to what's described in this article, it just needs to be adapted for whatever context menu solution you're using. Jesse
 
Posted by Jesse

Post a comment