Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable right click "Silverlight" popup in comboboxes

Hi I'm trying to get rid of the annoying "About Silverlight" context menu that pops up whenever you right click in a Silverlight application. I've added the usual ways:

In App.xaml
rootVisual.MouseRightButtonDown += ((s, args) => args.Handled = true);

and the same for all ChildWindows. The problem that persist is in all "pop up"-controls like comboboxes and datepicker calender popup. There I can't get rid of it. I would like to handle the right click in a style that I can make implicit for the entire application. Is this possible? Can I solve it some other smart way?

Best
Daniel

like image 983
Daniel Enetoft Avatar asked Sep 30 '10 07:09

Daniel Enetoft


2 Answers

The answer was to inherit the combobox and make a custom control like this:

public class CellaComboBox : ComboBox
{
    public CellaComboBox()
    {
        DropDownOpened += _dropDownOpened;
        DropDownClosed += _dropDownClosed;
    }

    private static void _dropDownClosed(object sender, EventArgs e)
    {
        HandlePopupRightClick(sender, false);
    }

    private static void _dropDownOpened(object sender, EventArgs e)
    {
        HandlePopupRightClick(sender, true);
    }

    private static void HandlePopupRightClick(object sender, bool hook)
    {
        ComboBox box = (ComboBox)sender;
        var popup = box.GetChildElement<Popup>();
        if (popup != null)
        {
            HookPopupEvent(hook, popup);
        }
    }

    static void HookPopupEvent(bool hook, Popup popup)
    {
        if (hook)
        {
            popup.MouseRightButtonDown += popup_MouseRightButtonDown;
            popup.Child.MouseRightButtonDown += popup_MouseRightButtonDown;
        }
        else
        {
            popup.MouseRightButtonDown -= popup_MouseRightButtonDown;
            popup.Child.MouseRightButtonDown -= popup_MouseRightButtonDown;
        }
    }


    static void popup_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        e.Handled = true;
    }

with the extension method for framworkelement looking like this:

public static class FrameworkElementExtensions
{
    public static TType GetChildElement<TType>(this DependencyObject parent) where TType : DependencyObject
    {
        TType result = default(TType);

        if (parent != null)
        {
            result = parent as TType;

            if (result == null)
            {
                for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); ++childIndex)
                {
                    var child = VisualTreeHelper.GetChild(parent, childIndex) as FrameworkElement;
                    result = GetChildElement<TType>(child) as TType;
                    if (result != null) return result;
                }
            }
        }

        return result;
    }
}

You need to handle the DatePicker in the same way but instead of DropDownOpened and DropDownClosed you use CalenderOpened and CalenderClosed

like image 62
Daniel Enetoft Avatar answered Nov 03 '22 14:11

Daniel Enetoft


C# Corner has an article for fixing the about popup on Silverlight 3:

Disable Context Menu in Silverlight 3 Application

like image 36
Luke Quinane Avatar answered Nov 03 '22 16:11

Luke Quinane