是否可以使用 BindingList使用 ListView 控件的方式与 ListBox 控件类似?
一个简单的例子:
BindingList<Dog> dogs = kennel.Dogs;
// Works great!
listBoxDogs.DataSource = dogs;
// Confuses me.
listViewDogs.? = dogs;
我已经探索了 listViewDogs.DataBindings 属性,但我无法梳理出与使用 DataSource 在 listBox 控件中看到的类似行为。
必须有一种更好的方法来更新 listViewDogs.Items 集合,然后捕获 dogs.ListChanged 事件并手动操作 listViewDogs.Items 集合。
我缺少什么?
A simple example:
BindingList<Dog> dogs = kennel.Dogs;
// Works great!
listBoxDogs.DataSource = dogs;
// Confuses me.
listViewDogs.? = dogs;
I have explored the listViewDogs.DataBindings property, but I have not been able to tease out of it similar behavior to what I am seeing with the listBox control using DataSource.
There has to be a better method of updating the listViewDogs.Items collection then catching the dogs.ListChanged event and doing the manipulation of the listViewDogs.Items collection by hand.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,列表视图不支持这种方式的数据绑定。
这是有关如何通过创建新控件来实现它的教程。
http://www.codeproject.com/KB/list/ListView_DataBinding.aspx
附:还有更多!
The list view unfortunately does not support databinding in that way.
here is a tutorial on how to implement it by creating a new control though.
http://www.codeproject.com/KB/list/ListView_DataBinding.aspx
ps. there are many more out there!
好吧,CodeProject 上接受的答案中的例子很糟糕。
所以,
但在哪里呢?
等等,这里有一个。我实现了一种更简单的方法,经过测试,最重要的是,可以使用
ListView
的扩展。嗯,它并不是真正可绑定的,但它支持任何实现
INotifyCollectionChanged
且其底层类型正在实现INotifyPropertyChanged
的泛型类,例如ObservableCollection
<代码>其中 T : INotifyPropertyChanged。关于使用此类,您只需要了解两件事。
开箱即用的
ListView
之间的主要区别是名为Collection
的新属性,其类型为INotifyCollectionChanged
。您仍然可以操作Items
集合,但我不建议这样做。预计您作为数据源提供的对象正在实现IEnumerable
接口,并且其基础类型也正在实现INotifyPropertyChanged
。我没有将Collection
属性限制到这些接口的原因是我想在分配属性时避免额外的转换。您始终可以为这些接口添加额外的检查并抛出 ArgumentException 以避免任何意外行为。还有一个名为
DefaultBrowsableState
的附加属性,它设置表示未指定BrowsableAttribute
的数据源对象的属性的列的默认可见性。原因是当您将此ListView
与另一个使用BrowsableAttribute
的控件(例如PropertyGrid
)一起使用时,并且您想要隐藏一些列表中的属性,同时保留它们在其他控件上的可见性。然后,您可以将DefaultBrowsableState
设置为 false,并将[Browsable(true)]
属性添加到您想要在列表中查看的所有属性。Well, the example on CodeProject from the accepted answer is terrible.
So,
But where?
Wait, here's one. I've implemented a way simplier, tested and, what's most important, ready to use extension of the
ListView
.Well, it's not really bindable but it supports any generic class that implements
INotifyCollectionChanged
whose underlying type is implementingINotifyPropertyChanged
, such asObservableCollection<T>
where T : INotifyPropertyChanged
.There are only two things you should know about using this class.
The main difference between Out of the box
ListView
is the new property calledCollection
of typeINotifyCollectionChanged
. You can still manipulate theItems
collection but I wouldn't recommend that. It is expected that the object you are providing as the data source is implementingIEnumerable
interface and also its underlying type is implementingINotifyPropertyChanged
. The reason I didn't constrained theCollection
property to these interfaces was that I wanted to avoid additional casting when assinging the property. You can always add additional checking for these interfaces and throw an ArgumentException just to avoid any unexpected behaviors.There's additional property called
DefaultBrowsableState
which sets the default visibility of the columns representing properties of the data source object that have noBrowsableAttribute
specified. The reason for this is when you using thisListView
along with another control which uses theBrowsableAttribute
(such asPropertyGrid
) and you want to hide some properties in the list while preserve their visibility on the other control. You can then set theDefaultBrowsableState
to false and add[Browsable(true)]
attribute to all properties you want to see in the list.