集合属性应为只读 [英] Collection properties should be read only

查看:136
本文介绍了集合属性应为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的FxCop为我的WPF MVVM组装,这让我




集合属性应该是只读的误差




但在我的财产,我需要RaisePropertyChangedEvent,现在如果我设置为仅通过删除其设定部分,我怎么能引发此事件读取属性。



语法是有点像这样

 公开名单的员工
{
{返回_employees; }

{
如果(的ReferenceEquals(_employees,值))
的回报;
_employees =价值;
RaisePropertyChanged(员工);
}
}


解决方案

您应该很少需要在集合提出一个PropertyChanged事件。使集合观察到的,这样它会通知每当项目添加或删除的任何绑定:

 公开的IList<员工>员工
{
获得;
私人集;
}

//在构造函数:
this.Employees =新的ObservableCollection<员工>();


I am using FxCop for my WPF MVVM assembly and it gives me the error

Collection properties should be read only

But in my property i need to RaisePropertyChangedEvent, now if i set the property to read only by removing its set section, how could i raise this event.

Syntax is somewhat like this

public List Employees
{
    get { return _employees; }
    set
    {
        if (ReferenceEquals(_employees, value))
            return;
        _employees = value;
        RaisePropertyChanged("Employees");
    }
}

解决方案

You should rarely need to raise a PropertyChanged event on a collection. Make the collection observable so that it notifies any bindings whenever items are added or removed:

public IList<Employee> Employees
{
    get; 
    private set;
}

// in your constructor:
this.Employees = new ObservableCollection<Employee>();

这篇关于集合属性应为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆