void
IToolboxService.SelectedToolboxItemUsed()
{
this.SelectedItemContainerUsed();
}

protected
virtual
void
SelectedItemContainerUsed()
{
this.SelectedItemContainer = null;
}
很显然,它缺省的方式将SelectedItemContainer属性值赋值为null, 这时候我们可以想到一个简便的办法是并不需要重载SelectedItemContainerUsed(), 而是修改SelectedItemContainer属性的set方法,找到上次尝试的代码,在SelectedItemContainer属性的set方法上写上:
if
(value
==
null
)
{
toolBox.SelectedIndex = -1;
}
不过后来我觉得还是象vs的工具箱一样,提供一个”Point”的“虚”工具更方便。这个功能只要在我们前面的代码上对几个小地方稍作修改就可以了,就不帖代码了。
接下来的步骤是实现能在PropertyGrid中随意修改任何控件的属性。要做到这点也很简便,只要为DesignSurface的SelectionService实现一个SelectionChanged事件就行了。
切换到Form1的代码窗口,为窗体类添加一个私有成员:
private
ISelectionService selectionService;
selectionService
=
surface.GetService(
typeof
(ISelectionService))
as
ISelectionService;
selectionService.SelectionChanged
+=
new
EventHandler(selectionService_SelectionChanged);
void
selectionService_SelectionChanged(
object
sender, EventArgs e)
{
object[] selection;
if (selectionService.SelectionCount == 0)
propertyGrid1.SelectedObject = null;
else
{
selection = new object[selectionService.SelectionCount];
selectionService.GetSelectedComponents().CopyTo(selection, 0);
propertyGrid1.SelectedObjects = selection;
}
}

好象是大功告成了,不过控件只能添加,却怎么删除不了。
当然,还有一些必不可少的功能,如将设计内容序列化到资源文件或者生成代码文件等等。胡适先生说:“自古成功尝试始”,我想一定会在以后几节的尝试中成功实现这些功能的。
本文介绍了如何在.NET 2.0中解决重复绘制控件的问题,并实现了在PropertyGrid中修改控件属性的功能。文中详细解释了通过继承ToolboxService类来避免重复绘制控件的方法,同时展示了如何为DesignSurface的SelectionService添加SelectionChanged事件以实现在PropertyGrid中实时显示并编辑所选控件的属性。
771

被折叠的 条评论
为什么被折叠?



