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

标签 silverlight silverlight-4.0

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

在 App.xaml

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


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

最好的事物
丹尼尔

最佳答案

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

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

关于silverlight - 禁用组合框中的右键单击 "Silverlight"弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3828514/

相关文章:

c# - 禁用 DataGrid 中的特定单元格编辑

c# - 如何在SilverLight中添加IFRAME?

c# - 如何通知已调用方法的对象

c# - Silverlight C# - 如何在事件完成之前保持循环进度?

wpf - Silverlight 4 中的像素字体

javascript - 如何判断用户退出Silverlight时是点击了“确定”还是“取消”?

Silverlight - 将 Listbox ActualWidth 绑定(bind)到 ListboxItem 宽度

silverlight - windows phone 上的音频路由?

silverlight - 通过 System.Windows.Automation 在 Chrome 中访问 Silverlight 窗口

windows-phone-7 - 将边框元素内的 TextBlock 文本居中