1

We have a class ModelBase that has defined a PropertyChangedEventHandler PropertyChanged. Then we inherit from this class, and using kind of a decorator pattern, add more functionality to it -- filtering, inserting, etc. We wrap the current object into another one each time we add stuff to it and copy all the properties so we can use them in the next class.

public class ModelBase<T> : IModel<T> where T : IView, new()
 {
   ...
   ...
   public event PropertyChangedEventHandler PropertyChanged;
   protected internal void RaisePropertyChanged(string propertyName)
   {
     if(PropertyChanged != null)
       PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
 }

 public class ViewModel : ModelBase<Grid>, IViewModel
 {
  protected internal ViewModel(iewModel viewModel)
            : base(viewModel)
  {
     // Copy all the properties of viewModel to the ones of this instance
     ...
     ...   
     PropertyChanged = viewModel.PropertyChanged;

  }

 }  

The issue is that when trying copy the PropertyChanged event handler the compiler throws the error ... can only appear on the left hand side of += or -= operator....

Is there a way how a can copy PropertyChanged property to the next decorator isntance?

Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101
  • See - http://stackoverflow.com/questions/660480/determine-list-of-event-handlers-bound-to-event - is this what you want to do? – ChrisF Nov 03 '11 at 14:37
  • 2
    That sounds like a bad idea. Are you sure you want to clone event handlers? – SLaks Nov 03 '11 at 14:37
  • You are doing something wrong. If the underlying object is a "living" one, that is, its properties may change and there are subscribers to its events, you can't do with a one-time clone: you need to catch the changes in the underlying object and reflect them in the wrapper object. If however your underlying object just contains the data, there have to be no event subscribers. – Vlad Nov 03 '11 at 14:45

2 Answers2

3

The field backing a field-like event is private; you can only access it within that class.

You could make a protected property in the base class that returns the delegate's contents:

protected PropertyChangedEventHandler ProeprtyChangedHandlers {
    get { return PropertyChanged; }
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
-1

This should work:

if (viewModel.PropertyChanged != null)
    this.PropertyChanged += viewModel.PropertyChanged;
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208