简体   繁体   中英

Calling the set method of a property when changing elements within that property

I have a class 'cropping' that contains 4 parameters:

public class Cropping
{
   public float Top { get; set; }
   public float Bottom { get; set; }
   public float Left { get; set; }
   public float Right { get; set; }
}

Within my UI class, I have an instance of the cropping class, that has a method call within the set block, to update the UI.

private Cropping croppingFactors;
/// <summary>Stores details on how to crop the image</summary>
public Cropping CroppingFactors
{
  get { return croppingFactors; }
  set 
  {
    croppingFactors = value;
    UpdateUIControls();
  }
}

The aim here is obviously to update the UI controls, everytime a change is made to one of the cropping factors, however, if I change an element within the Cropping class, ie a call to

CroppingFactors.Top = 5;

The set method of the CroppingFactors property is not being run.

How can I update the UI when I change an element within a class like this?

edit

I realise that the set property will not run because I have not set a new value to the cropping class. My question is: How do I invoke the 'UpdateUIControls()' method when I change any element within the Cropping class?

edit 2

Thanks to the answer given by Aran Mulholland - Here is my implementation for completeness:

I modified the Cropping class to implement the INotifyPropertyChanged class

public class Cropping : INotifyPropertyChanged
{
   private float top;
   public float Top
   {
     get { return top; }
     set
     {
       top = value;
       NotifyPropertyChanged();
     }
   }       

   //Same for bottom, left and right

   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged()
   {
     if(PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
}

And then in the UI class, I subscribed to the event:

public ImgHost()
{
  InitializeComponent();
  CroppingFactors.PropertyChanged += CroppingFactors_PropertyChanged;
}

private void CroppingFactors_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
  UpdateUIControls();
}

So that's my implementation, just for completeness. Thanks for all the answers!

Implement the INotifyPropertyChanged interface on the Cropping class. Subscribe to the property changed event from the UI class that is using the Cropping class as a property so that every time the property is set on any of the members of the Cropping class you can call the desired function.

You can always call the UpdateUIControls() whenever anything changes, but it depends what needs to trigger the change. Another alternative is add an event:

public event EventHandler PropertyUpdated;

And then whatever needs to update the control list, can call the method on the control in the event handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM