c# - 我如何检查 ObservableCollection 的 "CollectionChanged"事件是否为空

标签 c#

在 ObservableCollection 中,我如何检查 CollectionChanged 事件是否为 null, 该语句抛出语法错误

 if (studentList.CollectionChanged == null)

错误信息:

事件 'System.Collections.ObjectModel.ObservableCollection.CollectionChanged' 只能出现在 += 或 -= 的左侧

示例代码:

 public class School
    {
        public School()
        {
            studentList = new ObservableCollection<Student>();            
            //only when studentList.CollectionChanged is empty i want 
            // to execute the below statement
            studentList.CollectionChanged += Collection_CollectionChanged;
        }
        public ObservableCollection<Student> studentList { get; set; }
  }

最佳答案

您无法查看事件是否具有从拥有该事件的类外部附加的处理程序。您必须为要解决的问题找到不同的解决方案。

关于c# - 我如何检查 ObservableCollection 的 "CollectionChanged"事件是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327264/

相关文章:

c# - dapper:#tempDB 已创建但在下一个查询中不可用

c# - Mvvm wpf : update value binding before leaving focus of input element

c# - 如何更新Elasticsearch文档中的嵌套对象?

c# - 以编程方式删除 Windows Mobile 设备上的电子邮件和 SMS

c# - 如何获取文本模板转换工具包

java - 在 Azure 搜索上上传文档后,我可以立即查询吗?

c# - NetworkStream.DataAvailable 与 TcpClient.Available

c# - 在 C++ 代码中调用 TerminateThread 后在 C# 代码中检测到 FatalExecutionEngineError

c# - 跨多个控件实现 INotifyPropertyChanged 的​​优雅方式

c# - 如何报告实体对象的自定义(添加)计算属性已更改?