您的位置:首页 > 其它

如何实现自定义的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;}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: