如何实现自定义的DataSource
2004-02-03 23:35
375 查看
有时候我们希望能自己写一个component,并可以像DataSet、那样可以在设计时可以显示出其中的collection, 以及collection中的可绑定的属性。一下提供了一个简要的介绍:
IListSource,如果你的component本身不是一个Collection(本身不实现IList 或IBindingList)的话,你可以用这个接口返回一个实现该collection的IList .一个典型的例子就是DataSet。
ITypedList, 该接口用于返回需要显示在binding list picker中的属性。
一个简单的例子(摘自一个component,它实现了ITypedList, IBindingList).
#region ITypedList Members
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;}
}
IListSource,如果你的component本身不是一个Collection(本身不实现IList 或IBindingList)的话,你可以用这个接口返回一个实现该collection的IList .一个典型的例子就是DataSet。
ITypedList, 该接口用于返回需要显示在binding list picker中的属性。
一个简单的例子(摘自一个component,它实现了ITypedList, IBindingList).
#region ITypedList Members
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;}
}
相关文章推荐
- a universal class to complete import data from an excel file into a database
- [转帖]ASP.NET服务器端异步Web方法
- 开天辟地第一帖
- SQL Server唯一约束的使用
- 用户登录后重定向的一种新方法
- WinRAR 简体中文版下载地址(有3721, CNNIC, 百度, 新浪免疫程序)
- ASP 中 Split 函数的实例
- DW+ASP 玩转动态二级菜单
- SMART POINTER(智能指针)
- 进销存软件之OO设计--中间层处理(二)
- 数据结构与算法(C#实现)系列-----前言
- 数字签名(java)
- 从GBA开始,步入ARM开发的殿堂(初学者入门篇)
- 通用查询组件设计
- 数据结构与算法(C#实现)系列---N叉树(二)
- 数据结构与算法(C#实现)系列---N叉树(一)
- 数据结构与算法(C#实现)系列---广义树(二)
- 数据结构与算法(C#实现)系列---广义树(一)
- 数据结构与算法(C#实现)系列---树(三)
- 数据结构与算法(C#实现)系列---树(二)