禁用右键单击";Silverlight“;在组合框中弹出

禁用右键单击";Silverlight“;在组合框中弹出,silverlight,silverlight-4.0,Silverlight,Silverlight 4.0,嗨 我试图摆脱恼人的“关于Silverlight”上下文菜单,每当你右键点击Silverlight应用程序时,它就会弹出。我添加了常用的方法: 在App.xaml中 rootVisual.MouseRightButtonDown+=((s,args)=>args.Handled=true); 所有的儿童窗口也是如此。 持续存在的问题是所有“弹出”控件,如组合框和日期选择器日历弹出。在那里,我无法摆脱它。我希望以一种可以使整个应用程序隐式的方式来处理右键单击。这可能吗?我能用其他聪明的方法解决它

嗨 我试图摆脱恼人的“关于Silverlight”上下文菜单,每当你右键点击Silverlight应用程序时,它就会弹出。我添加了常用的方法:

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

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

最佳
Daniel

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


答案是继承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;
    }
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;
    }
}
public类CellaComboBox:ComboBox
{
公共cellacomboxe()
{
下拉打开+=\u下拉打开;
DropDownClosed+=\u DropDownClosed;
}
私有静态void\u dropDownClosed(对象发送方,事件参数e)
{
HandlePopupRightClick(发件人,错误);
}
私有静态void\u下拉打开(对象发送方,事件参数e)
{
HandlePopupRightClick(发件人,true);
}
私有静态无效HandlePoupPrightClick(对象发送器,布尔挂钩)
{
组合框=(组合框)发送方;
var popup=box.GetChildElement();
如果(弹出!=null)
{
HookPopupEvent(钩子,弹出);
}
}
静态void HookPopupEvent(bool hook,Popup)
{
如果(钩子)
{
popup.MouseRightButtonDown+=弹出窗口\u MouseRightButtonDown;
popup.Child.MouseRightButtonDown+=popup\u MouseRightButtonDown;
}
其他的
{
popup.MouseRightButtonDown-=popup\u MouseRightButtonDown;
popup.Child.MouseRightButtonDown-=popup\u MouseRightButtonDown;
}
}
静态无效弹出窗口\u MouseRightButtonDown(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{
e、 已处理=正确;
}
framworkelement的扩展方法如下所示:

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;
    }
}
公共静态类FrameworkElementExtensions
{
公共静态TType GetChildElement(此DependencyObject父级),其中TType:DependencyObject
{
t类型结果=默认值(t类型);
如果(父项!=null)
{
结果=作为TType的父级;
如果(结果==null)
{
for(int childIndex=0;childIndex

您需要以相同的方式处理日期选择器,但您使用CalendarOpen和CalendarClosed而不是DropDownOpen和DropDownClosed,谢谢您的回复。如果您在浏览器中运行应用程序,此解决方案将非常有效。不幸的是,此修复程序删除了应用程序的OOB可能性,OOB是客户的先决条件。您没有这样做吗需要创建自定义类。附加的行为可以正常工作。+1:这是一个非常好的修复。描述得很好。在SL AutocompleteComboBox上使用,效果相同。