using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
namespace FWindSoft.WinForm
{
///
/// 属性窗格绑定类
///
public class PropertyCollection : Collection, ICustomTypeDescriptor
{
public PropertyCollection() : base()
{
}
protected override void ClearItems()
{
foreach (var propertyItem in this)
{
propertyItem.Owner = null;
}
base.ClearItems();
}
protected override void InsertItem(int index, PropertyItem item)
{
if (item == null)
return;
this[index].Owner = this;
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
this[index].Owner = null;
base.RemoveItem(index);
}
protected override void SetItem(int index, PropertyItem item)
{
item.Owner = this;
this[index].Owner = null;
base.SetItem(index, item);
}
public PropertyItem this[string name]
{
get
{
return this.FirstOrDefault(t=> t != null && t.Name == name);
}
}
#region 实现接口
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
ArrayList props = new ArrayList();
for (int i = 0; i < this.Count; i++)
{
//判断属性是否显示
if (this[i].IsVisible)
{
PropDescriptor psd = new PropDescriptor(this[i], attributes);
props.Add(psd);
}
}
PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropDescriptor));
return new PropertyDescriptorCollection(propArray);
}
public PropertyDescriptorCollection GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
#endregion
}
}