Implement event for Property Changes - CSharp Custom Type

CSharp examples for Custom Type:Event

Description

Implement event for Property Changes

Demo Code

using System;/*  w w w  .  j a v  a 2s.  c om*/
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class Rectangle : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;
   private double width;
   private double height;
   public double Width
   {
      get { return width; }
      set
      {
         if (width == value)
         {
            return;
         }
         width = value;
         RaisePropertyChanged();
         RaisePropertyChanged(nameof(Area));
      }
   }
   public double Height
   {
      get { return height; }
      set
      {
         if (height == value)
         {
            return;
         }
         height = value;
         RaisePropertyChanged();
         RaisePropertyChanged(nameof(Area));
      }
   }
   public double Area => Width * Height;
   private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}
class PropertyChanges
{
   static void Main()
   {
      var rect = new Rectangle();
      rect.PropertyChanged += (sender, e) => Console.WriteLine(e.PropertyName);
      rect.Height = 20;
      rect.Width = 15;
   }
}

Result


Related Tutorials