locked
Context Menu not working in Firefox RRS feed

  • General discussion

  • I have been trying to provide a functionality of context menu using Silverlight, that works cross browser.

    I am able to do so in IE, but it is not working properly in Firefox. When I right click the first time, it shows the default menu, and only on the second click did I get the custom menu created by me along with default menu.

    Preventefault() is not working properly in FF. 

    Plz find the code below.

    <UserControl x:Class="SilverlightContextMenu.Page"
        xmlns="
    http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="
    http://schemas.microsoft.com/winfx/2006/xaml"
        Width="400" Height="300">
        <Grid Loaded="Page_Loaded">
            <Rectangle x:Name="LayoutRoot" Fill ="Green" ></Rectangle >
            <Popup x:Name="menu" MouseLeave="menu_MouseLeave">
                <StackPanel>
                    <Button x:Name="btnRed" Content="Red" Click="btnRed_Click"></Button>
                    <Button x:Name="btnWhite" Content="White" Click="btnWhite_Click"></Button>
                    <Button x:Name="btnBlue" Content="Blue" Click="btnBlue_Click"></Button>
                </StackPanel>
            </Popup>
        </Grid>
    </UserControl>


     
    using System.Windows.Browser;
    using System.Drawing;
    public partial class Page : UserControl
    {
       
        ContextMenuCatcher cm;
       
        public Page()
        {
            InitializeComponent(); 

        }
       
        private void btnBlue_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            LayoutRoot.fill = new SolidColorBrush(Colors.Blue);
            menu.IsOpen = false;
        }
       
        private void btnRed_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            LayoutRoot.fill = new SolidColorBrush(Colors.Red);
            menu.IsOpen = false;
        }
       
        private void btnWhite_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            LayoutRoot.fill = new SolidColorBrush(Colors.White);
            menu.IsOpen = false;
        }
       
        private void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            cm = new ContextMenuCatcher(LayoutRoot);
            cm.RightClick += new RightClickEventHandler(cm_RightClick);
            menu.IsOpen = false;
        }
       
        private void cm_RightClick(object sender, RightClickEventArgs e)
        {
            menu.HorizontalOffset = e.AbsolutePoint.X;
            menu.VerticalOffset = e.AbsolutePoint.Y;
            menu.IsOpen = true;
        }
       
        private void menu_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            menu.IsOpen = false;
        }
       
    }
    public class ContextMenuCatcher
    {
        public event RightClickEventHandler RightClick;
        public delegate void RightClickEventHandler(object sender, RightClickEventArgs e);
        UIElement ctrl;
       
        public ContextMenuCatcher(UIElement c)
        {
            ctrl = c;
           
            HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);
        }
       
        private void OnContextMenu(object sender, HtmlEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.OffsetX.ToString);
            System.Diagnostics.Debug.WriteLine(e.OffsetY.ToString);
            Point pt = new Point(e.OffsetX, e.OffsetY);
            e.PreventDefault();
            e.StopPropagation();
            if (RightClick != null) {
                RightClick(this, new RightClickEventArgs(ctrl, pt));
            }
        }
       
    }
    public class RightClickEventArgs
    {
       
        UIElement m_Source;
        public UIElement Source {
            get { return m_Source; }
            set { m_Source = value; }
        }
       
        Point m_RelativePoint;
        public Point RelativePoint {
            get { return m_RelativePoint; }
            set { m_RelativePoint = value; }
        }
       
        Point m_AbsolutePoint;
        public Point AbsolutePoint {
            get { return m_AbsolutePoint; }
            set { m_AbsolutePoint = value; }
        }
       
        internal RightClickEventArgs(UIElement source, Point absolutePoint)
        {
            this.Source = source;
            this.AbsolutePoint = absolutePoint;
            this.RelativePoint = GetPosition(source);
        }
       
        public Point GetPosition(UIElement relativeTo)
        {
            GeneralTransform transform = Application.Current.RootVisual.TransformToVisual(relativeTo);
            return transform.Transform(AbsolutePoint);
        }
    }

     Plz give any solution to overcome this problem.

    Thanks in advance,

    Tanusree

     

    Monday, April 27, 2009 1:56 PM

All replies

  • Hi, I got the same problem by using attaching js contextmenu event.

    If you find out a solution, could please share it? Thanks in advance!

    Regards!

    Monday, April 27, 2009 9:47 PM