1

我有一个对象,其数据成员实现了 INotifypropertychange 事件。我想单独维护一个对象列表,我不想反映属性更改。我该怎么做

4

2 回答 2

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"
于 2013-11-11T12:20:56.847 回答
0

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

公共类 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));
        }
    }
}
于 2013-11-15T09:11:53.460 回答