【问题标题】:Disable right click "Silverlight" popup in comboboxes在组合框中禁用右键单击“Silverlight”弹出窗口
【发布时间】:2010-09-30 07:07:54
【问题描述】:

嗨 我试图摆脱在 Silverlight 应用程序中右键单击时弹出的烦人的“关于 Silverlight”上下文菜单。我添加了常用的方法:

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

对于所有 ChildWindows 都是一样的。 持续存在的问题在于所有“弹出”控件,例如组合框和日期选择器日历弹出窗口。在那里我无法摆脱它。我想以一种我可以为整个应用程序隐含的样式处理右键单击。这可能吗?我可以用其他聪明的方法解决它吗?

最佳
丹尼尔

【问题讨论】:

    标签: silverlight silverlight-4.0


    【解决方案1】:

    答案是继承组合框并制作这样的自定义控件:

    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;
        }
    

    框架元素的扩展方法如下所示:

    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:这是一个很棒的修复。描述得很好。在具有相同效果的 SL AutocompleteComboBox 上使用。
    【解决方案2】:

    C# Corner 有一篇文章修复了 Silverlight 3 上的 about 弹出窗口:

    Disable Context Menu in Silverlight 3 Application

    【讨论】:

    • 感谢您的回复。如果您在浏览器中运行该应用程序,此解决方案将完美运行。不幸的是,此修复消除了应用程序的 OOB 可能性,而 OOB 是客户的先决条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-08-20
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多