Я разрабатываю компонент для себя. Одно из свойств компонента - «Статус», имеющее тип «StatusClass».
MyCode:
private StatusClass _mStatus=new StatusClass();
public StatusClass Status
{
get { return this._mStatus; }
set
{
this._mStatus = value;
this.Refresh();
}
}
проблема в том, что метод установки / "Refresh" не вызывается при изменении одного свойства StatusClass. Например:
myComponent.Status.proprety1 = 3; // the "Refresh method not call
но:
myComponent.Status = new StatusClass(); // the "Refresh method called
как я могу правильно определить свойство Status, чтобы при изменении его значения вызывалась функция установки.
Спасибо,





Метод обновления вызывается, когда экземпляр Status обновляется до нового объекта, как вы определили его в установщике this.Refresh();.
myComponent.Status = new StatusClass(); // the "Refresh method called
И он не будет вызываться этой строкой
myComponent.Status.proprety1 = 3; // the "Refresh method not call
поскольку здесь вы обновление собственностиStatus вместо самого объекта.
Чтобы достичь этого, даже при изменении свойств класса Status уведомление должно быть получено в классе invoker, затем вы реализуете интерфейс INotifyPropertyChanged, который помогает уведомить клиентов об изменении значения свойства. Вы можете прочитать об этом здесь.
public class StatusClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int proprety1
public int Proprety1
{
get
{
return this.proprety1;
}
set
{
if (value != this.proprety1)
{
this.proprety1 = value;
NotifyPropertyChanged();
}
}
}
}
Теперь в классе invoker вы можете определить объект как
public class DemoClass
{
private StatusClass _mStatus = new StatusClass();
public DemoClass()
{
_mStatus.PropertyChanged = (sender, args) => { this.Refresh(); }
}
}
Поэтому, когда теперь вызывается myComponent.Status.Proprety1 = 3;, будет вызываться Refresh, поскольку он подписался на изменение свойства StatusClass.