在我们开发自定义
控件的过程中,我们常常会给
控件添加集合属性。比如定制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;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
CPI.DataProcess.Forms
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[Serializable]
public class HeaderCollection:Collection<ColumnHeader>
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event CollectionChangeEventHandler ItemChanged;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public HeaderCollection()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void InsertItem(int index, ColumnHeader item)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
base.InsertItem(index, item);
ItemChanged(this,new CollectionChangeEventArgs(CollectionChangeAction.Add,item));
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void ClearItems()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
base.ClearItems();
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void RemoveItem(int index)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
base.RemoveItem(index);
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Remove, this[index]));
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void SetItem(int index, ColumnHeader item)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
base.SetItem(index, item);
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void OnItemPropertyChanged(Object sender)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
ItemChanged(sender,null);
}
}
}
这样我们的Collection就暴露出一个改变事件,在使用它的地方给它添加委托就可以了,例如:
public
void
OnCollectionPropertyChanged(Object sender,CollectionChangeEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Invalidate();
}
private
HeaderCollection _Headers
=
new
HeaderCollection();
Headers.ItemChanged
+=
new
CollectionChangeEventHandler(OnCollectionPropertyChanged);
当集合属性发生变化时就会触发事件通知自定义
控件重绘。