简体   繁体   English

为什么PropertyGrid不会引发键盘/鼠标事件?

[英]Why doesn't the PropertyGrid raise Keyboard/Mouse events?

Why does this never get called ? 为什么这永远不会被召唤?

        propertyGrid.KeyDown += new KeyEventHandler(propertyGrid_KeyDown);

        private void propertyGrid_KeyDown(object sender, KeyEventArgs e)
        {
           PoorLittleMethod(); //Never gets called
        }

This seems to be the same for Mouse event 这似乎与鼠标事件相同

I'veread on some forums that PGrid is tricky on raising such events as it Inherits them from Control but does not really Raise them. 我在一些论坛上已经知道PGrid在提出这样的事件时很棘手,因为它从Control继承它们但并没有真正提升它们。 is that true ? 真的吗 ? If yes, how to bypass that ? 如果是,如何绕过?

EDIT 1: 编辑1:
As this seems to be "regular", I find it very light from MS not to specify this explicitely on the MSDN Reference of the propertyGrid class and leave events "as is" as if they were usable, whereas they are not. 由于这似乎是“常规”,我发现MS非常轻,不要在propertyGrid类的MSDN参考上明确指定,并将事件“原样”保留为好像它们可用,而它们不是。 Tricky things like these are at least usually specified in "notes" inside the refs. 这些棘手的东西至少通常在refs里面的“notes”中指定。

EDIT 2: 编辑2:
I am presently coding a workaround. 我正在编写一个解决方法。 I'll be posting it soon. 我很快就会发布它。

The PropertyGrid 's KeyDown property is marked as Browsable(false) - presumably the conclusion we can take from this is that it is not supported in an of itself but is in fact present as a side-effect of its inheritance hierarchy. PropertyGridKeyDown属性被标记为Browsable(false) - 可能我们可以从中得出的结论是它本身不受支持,但事实上它是作为其继承层次结构的副作用。

Though, interestingly enough, its EditorBrowsable attribute (which is also a designer indicator, for Intellisense and the suchlike) is set as EditorBrowsableState.Advanced - where we would expect EditorBrowsableState.Never should the former presumption be true. 虽然,有趣的是,它的EditorBrowsable属性(也是Intellisense等设计器指示器)被设置为EditorBrowsableState.Advanced - 我们期望EditorBrowsableState.Never应该是前一个假设是真的。

Some information from MSDN forums outlines the why of this situation: 来自MSDN论坛的一些信息概述了这种情况的原因

From the tool UI Spy we can see the PropertyGrid is a just a panel and it consists of three Windows Controls. 从工具UI Spy我们可以看到PropertyGrid只是一个面板,它由三个Windows控件组成。 Our KeyDown event should be processed by the child control table. 我们的KeyDown事件应该由子控件表处理。 The structure: 结构:

-"pane" "PropertyGrid"
  --"pane" "Description Pane"
  --"table" "Properties Window"
  --"tool bar" "ToolBar"

The suggested solution (also provided in the MSDN link) to overcoming this is to use native system calls to retrieve window/control information, subclass NativeWindow and override the WndProc method to handle the events you like, KeyDown in this case. 建议的解决方案(也在MSDN链接中提供)来克服这个问题是使用本机系统调用来检索窗口/控制信息,子类NativeWindow并覆盖WndProc方法来处理您喜欢的事件,在这种情况下是KeyDown

您可以从PropertyGrid子类覆盖它以从Windows消息中获取一些关键信息

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

CSharp PropertyGrid Events CSharp PropertyGrid事件

// Property grid events can’t be easily subscribed to however there is way to get at the KeyUp event without impacting operation. 
// Note: The KeyDown event can be subscribed to in the same manner but the propertygrid is NOT updated with the key presses. 
// This code is added in hope it may help someone else solve the problem. It is not offered as a total solution.

// First define a class variable to indicate that events have been added.
private bool m_bPropertyGridEventsAdded = false;

public GlassInfoEntryPage(ViewBase view)
        : base(view)
    {
        InitializeComponent();

        // Subscribe to SelectedGridItemChanged
        m_PropertyGrid.SelectedGridItemChanged += M_PropertyGrid_SelectedGridItemChanged;
    }

// Now define a SelectedGridItemChanged Event Handler
private void M_PropertyGrid_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
        int nXlocation;
        int nYlocation;

        PropertyGrid oPropertyGrid;
        Control oControl;

        if (m_bPropertyGridEventsAdded == false)
        {
            oPropertyGrid = (PropertyGrid)sender;

            // Search the Property Grid for a PropertyGridView Control so events can be added to it
            for (nXlocation = 0; nXlocation < oPropertyGrid.Width; nXlocation += 10)
            {
                for (nYlocation = 0; nYlocation < oPropertyGrid.Height; nYlocation += 10)
                {
                    oControl = m_glassInfoPropertyGrid.GetChildAtPoint(new Point(nXlocation, nYlocation));

                    if (oControl != null)
                    {
                        if (oControl.GetType().ToString() == "System.Windows.Forms.PropertyGridInternal.PropertyGridView")
                        {
                            // Add Events here
                            oControl.Controls[1].KeyUp += MyCode_KeyUp;
                            m_bPropertyGridEventsAdded = true;
                            break;
                        }
                    }
                }

                if (m_bPropertyGridEventsAdded == true)
                {
                    break;
                }
            }
        }
    }        

// Handle the events
private void MyCode_KeyUp(object sender, KeyEventArgs e)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM