在我们开发自定义
控件的过程中,我们常常会给
控件添加集合属性。比如定制Grid
控件就会有Column集合。当集合属性发生变化时,比如添加新元素,删除新元素,我们要通知
控件去重绘以反映新的变化。我们可以创建一个集合类,在类里添加一个新的事件,并在集合发生变化时触发事件。.Net框架已经为我们提供了现成的委托声明,下面我们来看看怎么使用。
我创建一个名字叫HeaderCollection的类,派生于System.System.Collections.ObjectModel.Collection<ColumnHeader>。利用.Net已经声明的CollectionChangeEventHandler委托来声明一个事件,如下:
public delegate void CollectionChangeEventHandler (
Object sender,
CollectionChangeEventArgs e
)

CollecionChangeEventArgs的构造函数需要两个参数,一个枚举类型CollectionChangeAction,用它来指出发生了什么变化,包含添加,删除,整个集合发生变化三种动作,第二个参数传递发生变化的元素。
接下来我们重载Collection<T>的几个方法:InsertItem,ClearItems,RemoveItem在这些方法里触发事件,下边是这个类的完整描述:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CPI.DataProcess.Forms


{
[Serializable]
public class HeaderCollection:Collection<ColumnHeader>


{

public event CollectionChangeEventHandler ItemChanged;

public HeaderCollection()


{
}

protected override void InsertItem(int index, ColumnHeader item)


{
base.InsertItem(index, item);
ItemChanged(this,new CollectionChangeEventArgs(CollectionChangeAction.Add,item));
}

protected override void ClearItems()


{
base.ClearItems();
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
}

protected override void RemoveItem(int index)


{
base.RemoveItem(index);
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Remove, this[index]));
}

protected override void SetItem(int index, ColumnHeader item)


{
base.SetItem(index, item);
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
}

private void OnItemPropertyChanged(Object sender)


{
ItemChanged(sender,null);
}
}
}

这样我们的Collection就暴露出一个改变事件,在使用它的地方给它添加委托就可以了,例如:
public void OnCollectionPropertyChanged(Object sender,CollectionChangeEventArgs e)


{
Invalidate();
}
private HeaderCollection _Headers =new HeaderCollection();
Headers.ItemChanged+=new CollectionChangeEventHandler(OnCollectionPropertyChanged);

当集合属性发生变化时就会触发事件通知自定义
控件重绘。
在做控件的时候经常会遇到一个很烦人的问题,就是在测试工程中加入自己的控件,然后编辑属性的总是提示"Unable to cast object of type Xto type X"。看到这个问题,真是叫人哭笑不得啊。两个类名完全一样,何谓“无法转换” 。
既然出了问题,就得解决,一步一步跟吧。设好环境,调试控件的设计时行为,运行的结果竟然是完全没有问题。没有问题却成了最大的问题,无从下手了,上网查查吧,还好,发现很多人也都碰到这个问题。在微软的论坛里有一个解决的方法:
This particular problem apparently arises because of several factors:
The IDE caches versions of assemblies in the following folder:
"x\Documents and Settings\user\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies"
And at least in my case doesn't detect the particular assembly containing the type has a new version and it doesn't update the cache.
If you attach to a running instance of VS2005 and check what Modules are loaded (you may need to add the Modules window command from the Debug category in Tools > Customize...) you'll probably see two versions of your assembly are loaded, or even the same version of the assembly loaded twice: in both cases you'll see the same error message.
So first step, delete all folders in ProjectAssemblies. Next, make sure the IDE can only find ONE copy of your assembly, which is easier if you only have one copy of the DLL in your disk, and only one reference to it (or exactly the same reference from all projects) in your solution.
自己动手实践一下,删掉了cache的汇集,重新运行VS2005,问题解决了。