Search code examples
c#propertiesinotifypropertychangedpropertychangedpropertychangelistener

How to raise an event when an Bitmap property is changed?


I have an bitmap and an property like this :

   private Bitmap host_Bitmap;
   private Bitmap Host_Bitmap {get;set;}

How can I create an event when the host_Bitmap change?


Solution

  • If you want to take the simple route for one property, you add an event, and in the set you invoke it:

    public event EventHandler BitmapChanged;
    
    private Bitmap _hostBitmap;
    public Bitmap HostBitmap { get => _hostBitmap;
      set{
        _hostBitmap = value;
        BitmapChanged?.Invoke(this, EventArgs.Empty);
      }
    }
    

    If you want to pass more info about the event you can provide a modified EventArgs subclass and declare the BitmapChanged property type to be EventHandler<YourEventArgsSubclass>

    If you have a lot of properties to associate with events, look at implementing INotifyPropertyChanged