[英]Why is my ObservableCollection<T> not observed by my DataGridView?
I have to resort to the viewmodel firing an event, and on catching that event, refreshing the whole grid. 我必须求助于viewmodel触发一个事件,并在捕获该事件时刷新整个网格。 What is the point of something observable when it's not observed?
当不观察时,可观察的东西有什么意义?
This is how may main form starts up, first populating the grid, and repopulating it every time something is added to the collection: 这是可能启动主窗体的方式,首先填充网格,然后在每次向集合中添加内容时重新填充它:
private void MainForm_Load(object sender, EventArgs e)
{
FoundFilesBindingSource.DataSource = ViewModel;
// TODO Try get rid of event model.
ViewModel.FilesFound += (o, args) =>
{
if (FileInfosGrid.InvokeRequired)
{
FileInfosGrid.Invoke(new Action(() => FileInfosGrid.DataSource = ViewModel.FileInfos));
}
else
{
FileInfosGrid.DataSource = ViewModel.FileInfos;
}
};
}
On class ViewModel
, FileInfos
is declared as: 在类
ViewModel
, FileInfos
声明为:
public ObservableCollection<FindMatchViewModel> FileInfos { get; set; }
The ObservableCollection isn't working for a dataGridView because it doesn't implement IBindingList
. ObservableCollection不适用于dataGridView,因为它没有实现
IBindingList
。 You have to use a BindingList
instead. 您必须改为使用
BindingList
。 Make sure that your items implement INotifyPropertyChanged
if you want to reflect changes to your properties. 如果要反映对属性的更改,请确保您的项实现
INotifyPropertyChanged
。
The problem is that ObservableCollection is designed for Wpf Controls. 问题在于ObservableCollection是为Wpf控件设计的。
Notice that a BindingList
doesn't support sorting or filter your data and reflect this. 请注意,
BindingList
不支持对数据进行排序或过滤并反映这一点。 This behaviour is only supported by DataTable or custom List implementations. 仅DataTable或自定义List实现支持此行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.