简体   繁体   English

为什么我的ObservableCollection <T> 我的DataGridView没有观察到?

[英]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: 在类ViewModelFileInfos声明为:

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.

相关问题 为什么我的ObservableCollection序列化不起作用? - Why my ObservableCollection serialization doesn't work? 为什么将项目添加到 ObservableCollection 时我的视图没有更新 - Why doesn't my view update when adding items to the ObservableCollection 为什么我的ObservableCollection无法观察? - Why is my ObservableCollection going unobserved? 为什么我的ObservableCollection没有在ListBox中刷新? - why my ObservableCollection is not refreshed in the ListBox? 为什么我的RelayCommand不会触发并且ObservableCollection捕获所选值? - Why won't my RelayCommand fire and ObservableCollection catch selected value? 为什么我的ObservableCollection不会 <MenuItem> 更新RaisePropertyChanged? - Why won't my ObservableCollection<MenuItem> update on RaisePropertyChanged? 为什么我的DataGridView中的此Button没有单击? - Why doesn't this Button in my DataGridView click? 为什么我的ObservableCollection无法更新我的UI? - Why isnt my ObservableCollection updating my UI? 为什么当我在 DataGrid 中做了一些更改时,我的 ObservableCollection 没有任何更改? - Why I don't have any changes in my ObservableCollection when I have made some in my DataGrid? 为什么我的第二个ObservableCollection获取更新? - Why my second ObservableCollection is getting update?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM