不应触发列表的基础对象的InotifyPropertychanged事件

时间:2013-11-11 11:57:19

标签: c# list inotifypropertychanged

我有一个对象,其数据成员已经实现了INotifypropertychange事件。我想分别维护一个对象列表,我不想反映属性的变化。我该怎么做

2 个答案:

答案 0 :(得分:0)

包装/代理示例:

class MyItem : INPC
{
    public string Name { get { ... } set { this.name = value; raisePropChanged("Name") } } ....
}

var item = new MyItem();
collection.Add(item);

item.Name = "John"; // notifies whoever listens on collection



class MyItemWrapper
{
    private MyItem theBrain;

    public string Name { get{return theBrain.Name;} set{theBrain.Name = value;}}
}

var item = new MyItem();
var wrapped = new MyItemWrapper { theBrain = item };

collectionOne.Add(item);
collectionTwo.Add(wrapped);

item.Name = "John";
// notifies whoever listens on collectionOne
// but whoever listens on collectionTwo will not get any notification
// since "wrapper" does not notify about anything.
// however, since wrapper forwards everything to 'brain':

var name = wrapped.Name; // == "John"

答案 1 :(得分:0)

调用函数GetDeepCopy()来获取不会引发INPC的对象。

public class ValidationModel:INotifyPropertyChanged     {

    private string _validationName;

    public string validationName
    {
        get { return _validationName; }
        set { _validationName = value; NotifyPropertyChanged("ValidationName"); }

    }



    public ValidationModel GetDeepCopy()
    {
        var model = new ValidationModel();

        model.validationName = validationName;

        return model;

    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyname)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}