我希望仅当某些其他属性被修改时,才能将 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; }
}
最佳答案
无需使用 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; }
}
关于C# - 当其他属性被修改时自动更新对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35049480/