如何通过自定义设计器管理propertygrid窗口? [英] How do i manage the propertygrid window from my custom designer?

查看:70
本文介绍了如何通过自定义设计器管理propertygrid窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个自定义设计器(如窗体设计器),该设计器可用于在设计时设计自定义控件(请参见屏幕截图).

http://filesmelt.com/dl/ydi_sc_4.png [

I am devloping a custom designer (like the form designer), which can be used to design custom controls at designtime (see screenshot).

http://filesmelt.com/dl/ydi_sc_4.png[^]

The problem is that i want to show the propertyes of the currently selected object in the propertygrid window in VS, and i can not figure out how to do that. Is there a way to do so, or do i have to hack my way into it?

Thanks in advance, TheBlackCap.

推荐答案

这取决于您要自定义属性的数量.如果只需要以与Visual Studio相同的方式显示对象,则只需将属性System.Windows.Forms.PropertyGrid.SelectedObject分配给要显示的对象的引用,请参见^ ].

您可能会注意到,某些对象不是以默认方式显示的,而是以某些高级"方式显示的.例如,如果您具有类型System.Drawing.Point的属性,它将显示为两级树.如果您创建看似相同的结构并将其显示为类的公共属性,则不会这样显示. .NET库中定义的某些属性显示了特殊的编辑器.如何在自己的代码中执行类似的操作?这不是那么容易.

我已经完成了所有这些工作,并详细介绍了我过去的解决方案中涉及的技术:
如何在单击PropertyGrid时获得响应 [ ^ ],
如何在单击PropertyGrid时获得响应 [ ^ ].

请参阅这些解决方案和讨论.您将能够看到应该使用哪些技术和接口/类,并自己创建此类解决方案.这个想法是:PropertyGrid本身没有被修改.相反,您创建了一个名为PropertySurrogate的特殊类,该类使用您的真实"对象和Reflection进行初始化,以定义所选对象表示形式的所有细节:要显示的成员,允许编辑的成员以及编辑.

该技术是如此复杂,以至于需要花费大量篇幅来解释该技术.您可能需要也可能不需要.我当前解决方案的第一段中说明的默认用法足以满足您的需求.

—SA
It depends on how much you want to customize the properties. If you simply need to show your object in the same way Visual Studio does, you just need to assign the property System.Windows.Forms.PropertyGrid.SelectedObject to the reference of your object you want to show, see http://msdn.microsoft.com/en-us/library/system.windows.forms.propertygrid.aspx[^].

You might noticed that some objects are shown not in the default way, but in some "advanced" way. For example, if you have a property of the type System.Drawing.Point, it will be shown as a two-level tree. If you create seemingly identical structure and show it as a public property of your class, it won''t be shown like this. Some properties defined in .NET library show special editors. How to do something like this in your own code? This is not so easy.

I have done all this and provided a detailed overview of the techniques involved in my past solutions:
How to get response when click PropertyGrid[^],
How to get response when click PropertyGrid[^].

Please see those solutions and the discussions. You will be able to see what techniques and interfaces/classes should be used and create such solutions by yourself. The idea is: PropertyGrid itself is not modified. Instead, you have create a special class I called PropertySurrogate which is initialized using your "real" object and Reflection to define all the detail of the presentation of your selected object: which members to show, which members are allowed to be editable and the editors.

The technique is so complex that it would take a whole big article to explain the techniques. You may or may not need it. The default usage explained in the first paragraph of my present solution could be sufficient for your purposes.

—SA


这篇关于如何通过自定义设计器管理propertygrid窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆