禁用Silverlight组合框中的右键单击弹出窗口

5

你好,我正在尝试摆脱在Silverlight应用程序中右键单击时弹出的烦人的"关于Silverlight"上下文菜单。我已经按照通常的方法添加了以下代码:

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

并在所有ChildWindows中也加入同样的代码。 问题仍然存在于所有"弹出"-控件中,例如组合框和日期选择器弹出的日历。我无法摆脱它。我希望以一种可以为整个应用程序隐式设置的方式处理右键单击。这可能吗?还有其他聪明的解决方法吗?

最好的祝福
丹尼尔

2个回答

6
答案是继承combobox并制作一个自定义控件,如下所示:
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;
    }

使用 FrameworkElement 的扩展方法如下所示:
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;
    }
}

您需要以相同的方式处理DatePicker,但是使用CalenderOpened和CalenderClosed而不是DropDownOpened和DropDownClosed。


1
不需要创建自定义类,使用附加行为即可。 - SergioL
+1:这是一个非常棒的修复。描述得很清楚。在使用SL AutocompleteComboBox时具有相同的效果。 - Dr. Andrew Burnett-Thompson

2

1
感谢您的回复。如果您在浏览器中运行应用程序,此解决方案将完美地工作。不幸的是,这个修复程序会删除应用程序的OOB功能,而OOB是客户的先决条件。 - Daniel Enetoft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接