C#.Net组件开发(高级篇) - 自定义CollectionEditor编辑器
自定义CollectionEditor编辑器
下图是DataGridViewColumnCollectionEditor, 用于编辑DataGridView的Columns。
DataGridView.Columns属性定义:
//
// 摘要:
// Gets a collection that contains all the columns in the control.
//
// 返回结果:
// The System.Windows.Forms.DataGridViewColumnCollection that contains all the
// columns in the System.Windows.Forms.DataGridView control.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design, Version=
[MergableProperty(false)]
public DataGridViewColumnCollection Columns { get; }
由此可见,Columns 属性是由DataGridViewColumnCollectionEditor 设计器进行设计的。
在VS内不能查看DataGridViewColumnCollectionEditor 的元数据一探究竟,非常遗憾!从网上找到资料,该编辑窗体是公用的,只需要继承CollectionEditor基类就能实现自己的设计器。
另外,DataGridViewColumnCollection 继承BaseCollection基类 ,并实现IList接口。
// 摘要:
// Represents a collection of System.Windows.Forms.DataGridViewColumn objects
// in a System.Windows.Forms.DataGridView control.
[ListBindable(false)]
public class DataGridViewColumnCollection : BaseCollection, IList, ICollection, IEnumerable
{
}
自定义CollectionEditor编辑器
/// <summary>
/// 自定义CollectionEditor编辑器
/// </summary>
public class MyCollectionEditor : CollectionEditor
{
public MyCollectionEditor(Type type)
: base(type)
{ }
/// <summary>
/// 限制一次选一个实例
/// </summary>
/// <returns></returns>
protected override bool CanSelectMultipleInstances()
{
return false;
}
/// <summary>
/// 指定创建的对象类型
/// </summary>
/// <returns></returns>
protected override Type CreateCollectionItemType()
{
return typeof(CSFrameworkNode);
}
protected override object CreateInstance(Type itemType)
{
//创建一个实例
CSFrameworkNode o = (CSFrameworkNode)itemType.Assembly.CreateInstance(itemType.FullName);
IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
host.Container.Add(o);//重要!自动生成组件的设计时代码!
//或者:
//this.Context.Container.Add(o);//重要!自动生成组件的设计时代码!
return o;
}
protected override void DestroyInstance(object instance)
{
base.DestroyInstance(instance);//重要!自动删除组件的设计时代码!
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
自定义MyNodeCollection对象集合
/// <summary>
/// 自定义BaseCollection对象集合,实现IList接口
/// 如Object Collection Editor窗体的Add/Delete钮不可用是因为没有实现IList接口
/// </summary>
public class MyNodeCollection : BaseCollection, IList
{
private ArrayList _innerList;
public MyNodeCollection()
{
_innerList = new ArrayList();
}
protected override ArrayList List
{
get
{
return (ArrayList)_innerList;
}
}
#region IList Members
public int Add(object value)
{
return this.List.Add(value);
}
public void Clear()
{
this.List.Clear();
}
public bool Contains(object value)
{
return this.List.Contains(value);
}
public int IndexOf(object value)
{
return this.List.IndexOf(value);
}
public void Insert(int index, object value)
{
this.List.Insert(index, value);
}
public bool IsFixedSize
{
get { return this.List.IsFixedSize; }
}
public void Remove(object value)
{
this.List.Remove(value);
}
public void RemoveAt(int index)
{
this.List.RemoveAt(index);
}
public object this[int index]
{
get
{
return List[index];
}
set
{
List[index] = value;
}
}
#endregion
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
定义一个MyNodeCollection类型的属性,并定义属性的编辑器
/// <summary>
/// 用于测试打开Collection Editor的编辑器窗体
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]//属性编辑器
public MyNodeCollection MyNodeCollection
{
get
{
return _MyNodeCollection;
}
set
{
_MyNodeCollection = value;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
扫一扫加作者微信