C# - 修改其他属性时自动更新对象属性

时间:2016-01-27 22:36:47

标签: c#

我希望能够在修改某些其他属性时自动将UpdateDate字段的值更新为当前日期时间。对于这个例子 - 标题。如果一个类包含许多属性,其中一半属性应该触发UpdateDate值更改,那么这样做的可行方法是什么?

    public class Ticket
    {
       public Ticket() { }

       public int TicketID { get; set; }
       public string Title { get; set; }
       public string Description { get; set; }
       public DateTime UpdateDate { get; set; }
    }

4 个答案:

答案 0 :(得分:3)

无需使用INotifyPropertyChanged。这是一个例子,如果“Title”属性发生变化,UpdateDate将被设置为“DateTime.Now”

public class Ticket
{
   public int TicketID { get; set; }
   private string title;
   public string Title
   {
        get { return title; }
        set
        {                
            title = value;
            UpdateDate = DateTime.Now;
        }
    }
    public string Description { get; set; }
    public DateTime UpdateDate { get; set; }   
}

答案 1 :(得分:1)

你仍然需要编写一些每个属性的goo代码,但INotifyPropertyChanged interface为此提供了一种模式。

答案 2 :(得分:1)

我看到这是一个老问题,但在搜索相同的答案后,我想出了一个相对干净(但不是完全自动)的解决方案。

您可以创建两个类来帮助您解决这个问题。第一个是 Recorder,它只是保持和更新 DateTime:

public class Recorder {
    public Recorder(DateTime updateDate) {
        UpdateDate = updateDate;
    }

    public void Update() {
        UpdateDate = DateTime.Now;
    }

    public DateTime UpdateDate { get; private set; }
}

然后,Recorded<T> 将在其值更改时更新 Recorder

public class Recorded<T> {
    private readonly Recorder recorder;
    private T value;

    public Recorded(Recorder recorder, T value = default(T)) {
        this.recorder = recorder;
        this.value = value;
    }

    public T Value {
        get => value;
        set {
            this.value = value;
            recorder.Update();
        }
    }
}

您原来的 Ticket 需要一些更改,但可以这样实现:

public class Ticket
{
   private readonly Recorder recorder;
   private readonly Recorded<int> ticketId;
   private readonly Recorded<string> title;
   private readonly Recorded<string> description;

   public Ticket(int ticketId, string title, string description, DateTime updateDate)
   {
       recorder = new Recorder(updateDate);
       this.ticketId = new Recorded<int>(recorder, ticketId);
       this.title = new Recorded<string>(recorder, title);
       this.description = new Recorded<string>(recorder, description);
   }

   public int TicketID { get => ticketId.Value; set => ticketId.Value = value; }
   public string Title { get => title.Value; set => title.Value = value; }
   public string Description { get => description.Value; set => description.Value = value; }
   public DateTime UpdateDate { get => recorder.UpdateDate; }
}

答案 3 :(得分:0)

只需创建一个继承自INotifyPropertyChanged接口的基类,如下所示:

public abstract class BaseViewModel : INotifyPropertyChanged
{
    #region members
    protected IUnitOfWork UnitOfWork;
    #endregion

    public BaseViewModel()
    {            
    }

     //basic ViewModelBase
    internal void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
    }
    public event PropertyChangedEventHandler PropertyChanged;

}

然后你可以在你的具体类中使用:

public class TransactionItemViewModel : BaseViewModel
{
    int _Quantity;
    public int Quantity
    {
        get
        {
            return _Quantity;
        }
        set
        {
            if (_Quantity != value)
            {
                _Quantity = value;
                RaisePropertyChanged("Quantity");
                RaisePropertyChanged("TotalSalePrice");
            }
        }
    }

    public decimal TotalSalePrice
    {
        get
        {                                
            return 100 * Quantity;
        }
    }
}