编写与.net属性窗口交互的RAD组件(五)

八、 提供可交互的属性视图

当你在 Visual C# .NET 中创建一个项目的时候,你可能会注意到属性窗口的工具栏上有一个像闪电的按钮,按下这个按钮属性窗口就会切换到事件视图,这样就可以来编辑事件处理了。

属性窗口的视图来自“属性页( Property Tabs )”,因此视图使用的最主要的类是 PropertyTab ,命名空间是 System.Windows.Forms.Design 。一个属性页可以和一个特别的组件、设计文档关联起来,或者是可以使用的静态关联。和组件或文档关联起来的属性页在类上用 PropertyTabAttribute 特性来指定。这个特性指定要创建的 Tab 的类型,它在属性窗口上是否显示由 PropertyTabAttribute 的 PropertyTabScope 参数来指定。指定为 Component 范围的属性页的可见性由有 PropertyTabAttribute 特性的组件的可见性来决定。 Document 范围的属性页则可以在当前项目的设计中都可见。他的默认值是 PropertyTabScope.Component 。

举一个例子来说,看看“ FunkyButton ”项目。 FunkyButton 是一个扩展了 PropertyTab 的 UserControl ,而且可以让我们把控件定为不规则的多边形。

** 图 6. FunkyButton **

当前选择的属性页就是属性窗口从被选择的控件的属性中得到的。属性页因此就允许来操纵显示属性的不同集合。 Events 页就是像属性一样以某种方式来处理事件。在这个例子中,属性页就创建了表示控件顶点的属性。

.NET framework 中的属性用 PropertyDescriptor 类来封装。 PropertyDescriptor 本身是一个抽象类, framework 中由他派生的类提供了访问组件的开放属性的方法。不过,属性窗口是直接作用在 PropertyDescriptor 上,而不是直接作用在属性上。因此,我们就可以写自己的 PropertyDescriptor 来做一些特殊的工作。在这个例子里,我们就有一个属性表示控件的顶点数,另一个就表示每一个顶点。再次注意一下,我们在属性窗口上增加页并不相应的作用在其他对象上。

当属性窗口向 PropertyTab 询问 Properties 的时候,它就调用 GetProperties 方法。对于我们的示例程序,这个方法就像下面的一样:

public override PropertyDescriptorCollection

GetProperties(ITypeDescriptorContext context, object component,

Attribute[] attrs)

{

// our list of props.

//

ArrayList propList = new ArrayList();

// add the property for our count of vertices

//

propList.Add(new NumPointsPropertyDescriptor(this));

// add a property descriptor for each vertex

//

for (inti = 0; i < ((FunkyButton)component).Points.Count; i++)

{

propList.Add(new VertexPropertyDescriptor(this,i));

}

// return the collection of PropertyDescriptors.

PropertyDescriptor[] props =

(PropertyDescriptor[])propList.ToArray(typeof(PropertyDescriptor));

return new PropertyDescriptorCollection(props);

}

GetProperties 仅仅是返回一些属性描述的集合。 PropertyDescriptors 是相当的简单,仔细查看这些代码以了解他们是怎么工作的。

FunkyButton 同时示例了下拉列表编辑器的实现。对于每一个点,我们不是简单的输入坐标的 X 和 Y 值,我们会图示 FunkyButton 的形状,而且可以用图形化的方法改变点的位置。这样设置的编辑样式更加地友好。

** 图 7. 图形化的点向量 **

由于订制的 PropertyTab 提供了属性,重载这个属性的编辑器也是很容易的。只要简单地重载 PropertyDescriptor 的 GetEditor 方法,然后返回订制组件的实例就可以了。

public override object GetEditor(Type editorBaseType)

{

// make sure we're looking for a UITypeEditor.

//

if (editorBaseType == typeof(System.Drawing.Design.UITypeEditor))

{

// create and return one of our editors.

//

if (editor == null)

{

editor = new PointUIEditor(owner.target);

}

return editor;

}

return base.GetEditor(editorBaseType);

}

设计编辑器同样简单。编辑器就是一个简单的 UserControl ,所以我们就可以像设计其他的 windowsForms 对象一样来做。

** 图 8. Designing the editor **

最后,当用户在属性窗口中点击下拉箭头时,我们的编辑器就可以将刚才创建 UI 编辑器弹出来了。 PointUIEditor 中的 UITypeEditor.EditValue 重载后就可以实现了。

public override object EditValue(

ITypeDescriptorContext context,

IServiceProvider sp, object value)

{

// get the editor service.

IWindowsFormsEditorService edSvc =

(IWindowsFormsEditorService)sp.GetService(typeof(IWindowsFormsEditorService));

// create our UI

if (ui == null)

{

ui = new PointEditorControl();

}

// initialize the ui with the settings for this vertex

ui.SelectedPoint = (Point)value;

ui.EditorService = edSvc;

ui.Target = (FunkyButton)context.Instance;

// instruct the editor service to display the control as a

// dropdown.

edSvc.DropDownControl(ui);

// return the updated value;

return ui.SelectedPoint;

}

九、 我们同样可以使用它

在你自己的应用中可以拥有和 IDE 属性窗一样的特性。把 System.Windows.Forms.PropertyGrid 的控件,添加到 IDE 中的 ToolBox 中,通过获取在 ToolBox 的 Component 标签里的 PropertyGrid 。

PropertyGrid 和其他的控件工作是一样的。你可以 anchor 或者是 Dock 他,改变它的色彩等。下面的列表列出了 PropertyGrid 的一些有趣的属性。

• SelectedObject

|

PropertyGrid 要显示的对象

---|---

• ToolbarVisible

|

显示或者隐藏 PropertyGrid 顶端的 Toolbar

• HelpVisible

|

显示或者隐藏 PropertyGrid 底端的帮助文本

• PropertySort

|

设置 PropertyGrid 的排序类型 (categorized, alphabetical, etc.).

这些属性都可以在设计时设置。在运行时,可以操作 PropertyGrid 让他显示的你的对象。下面是显示一个 button 的例子。在这个例子中, PorpertyGrid 的帮助和 toolbox 都被隐藏了。就像上面提到的,你可以设置他自己的属性。

** 图 9. 隐藏了 toolbar 和帮助信息的 PropertyGrid **

十、 结论

NET framework 和 Visual Studio .NET 给属性窗口增加了相当多的功能。由于属性窗口是 RAD 的核心,这些特性可以在保持易用性的同时有很多的扩展,也因此在 Visual Basic 中用的很普遍。就像可以在我们的程序中使用 PropertyGrid ,我们可以把更多的时间放在如何写好程序上,从而简化我们的 UI 工作。

-----------------------------------------------------------------------------------------

<<<<<<<<<<<<<<<<<<<待续>>>>>>>>>>>>>>>>>>>>>>

Published At
Categories with Web编程
Tagged with
comments powered by Disqus