PropertyGrid控件(9) 自定义 UITypeEditor

概要:自定义 PropertyGrid 属性的Editor和PropertyTab。

Providing a Custom UI for Your Properties

As mentioned earlier, the .NET Framework types use the TypeConverter and UITypeEditor classes (along with other classes) to provide PropertyGrid editing support. The Support for Custom Types section looked at using a TypeConverter; you can also use the UITypeEditor class to perform your own PropertyGrid customization.

You can provide a small graphical representation along with the property value in the PropertyGrid, similar to what is provided for the Image and Color classes. To do this for your customization, inherit from UITypeEditor, override GetPaintValueSupported and return true. Next, override the UITypeEditor.PaintValue method, and in your method, use the PaintValueEventArgs.Graphics parameter to draw your graphic. Finally, apply the Editor attribute to the class or property that uses your UITypeEditor class.

The following screen shot shows what your results might look like.

propertygrid, custom uitypeeditor

Figure 8. Displaying a custom graphic for a property in the PropertyGrid

You can also provide your own drop-down list control, similar to what the Control.Dock property uses to give the user docking selections. To do this, inherit from UITypeEditor, override GetEditStyle, and return a UITypeEditorEditStyle enum value, such as DropDown. Your custom drop-down control must inherit from Control or a Control-derived class (like UserControl). Next, override the UITypeEditor.EditValue method. Use the IServiceProvider parameter to call the IServiceProvider.GetService method to get an IWindowsFormsEditorService instance. Finally, call the IWindowsFormsEditorService.DropDownControl method to show your custom drop-down list control. Remember to apply the Editor attribute to the class or property that uses your UITypeEditor class.

The following screen shot shows what your results might look like.

propertygrid, custom uitypeeditor,drop-down

Figure 9. Displaying a custom drop-down control for a property in the PropertyGrid

In addition to using the TypeEditor and UITypeEditor classes, the PropertyGrid can also be customized to display additional property tabs. Property tabs inherit from the PropertyTab class. You might have seen a custom PropertyTab if you've used the property browser in Microsoft Visual C#™ .NET -- the Events tab (the button with lightning bolt) is a custom PropertyTab. The following screen shot shows another example of a custom PropertyTab is shown below. This PropertyTab provides the ability to create a custom button shape by editing bounding points of a button.

propertygrid, property tab

Figure 10. Displaying a custom tab in the PropertyGrid

You can find more information about using the UITypeEditor class to customize the PropertyGrid, along with code examples of the above custom user interfaces, Shawn Burke's article, Make Your Components Really RAD with Visual Studio .NET Property Browser.

Conclusion

The ProperyGrid control provided by the .NET Framework provides a rich editing experience that you can use to enhance your user interface. With your new knowledge on how easy the PropertyGrid is to customize, you will be using the PropertyGrid throughout your applications. In addition, since the Visual Studio .NET property browser is based upon the PropertyGrid, you can use these techniques to provide a richer design time experience.

 

 


编者或作者: 我有闲    收录日期: 2008-12-22
参考或来源: 我有闲IT教程网 WoYouXian.net

上一页: PropertyGrid控件(8) 下拉框属性 返回上级目录: IT知识文章 下一页: PropertyGrid控件(10) 显示动态自定义属性


© 2008 woyouxian.net 版权所有 Contact Us