如何实现自定义的DataSource

有时候我们希望能自己写一个component,并可以像DataSet、那样可以在设计时可以显示出其中的collection, 以及collection中的可绑定的属性。一下提供了一个简要的介绍:

IListSource,如果你的component本身不是一个Collection(本身不实现IList 或IBindingList)的话,你可以用这个接口返回一个实现该collection的IList .一个典型的例子就是DataSet。
ITypedList, 该接口用于返回需要显示在binding list picker中的属性。

一个简单的例子(摘自一个component,它实现了ITypedList, IBindingList).

#region ITypedList Members<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)

{

     MessageBox.Show("sdfasf");

     // TODO:  Add UserControl1.GetItemProperties implementation

     PropertyDescriptorCollection col = TypeDescriptor.GetProperties(this);

     PropertyDescriptorCollection colnew = new PropertyDescriptorCollection(null);

     MyBindableAttribute myAttr = new MyBindableAttribute();

     foreach(PropertyDescriptor prop in col)

     {

         if (prop.Attributes.Contains(myAttr))

              colnew.Add(prop);

     }

     return colnew;

 

}

 

public string GetListName(PropertyDescriptor[] listAccessors)

{

     MessageBox.Show("GetListName");

     // TODO:  Add UserControl1.GetListName implementation

     foreach(PropertyDescriptor prop in listAccessors)

         MessageBox.Show(prop.DisplayName);

     MessageBox.Show("GetListName");

 

     return "Hello";

}

 

#endregion

 

class MyBindableAttribute : Attribute {}

 

 

[MyBindable]

public string Text

{

     get  {return string.Empty;}

}

 

转载于:https://www.cnblogs.com/jonnyyu/archive/2004/02/03/763.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值