EditableItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. namespace FWindSoft.Wpf
  8. {
  9. public class EditableItem : INotifyPropertyChanged,IEditableObject
  10. {
  11. public EditableItem()
  12. {
  13. IsEdited = false;
  14. NewAdd = false;
  15. IsListenEditing = false;
  16. }
  17. #region 实现INotifyPropertyChanged
  18. public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
  19. protected virtual void RaisePropertyChanged(String propertyName = "")
  20. {
  21. if (IsListenEditing)
  22. {
  23. this.IsEdited = true;
  24. }
  25. if (PropertyChanged != null)
  26. {
  27. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  28. }
  29. }
  30. protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
  31. {
  32. if (null != propertyExpression)
  33. {
  34. var memberExpression = propertyExpression.Body as MemberExpression;
  35. if (memberExpression != null)
  36. RaisePropertyChanged(memberExpression.Member.Name);
  37. }
  38. else
  39. {
  40. throw new ArgumentNullException("propertyExpression");
  41. }
  42. }
  43. #endregion
  44. #region 实现EditableObject,UI驱动数据
  45. private bool m_TempEdit;
  46. public virtual void BeginEdit()
  47. {
  48. if (IsListenEditing)
  49. {
  50. m_TempEdit=IsEdited;
  51. //IsEdited = true;
  52. }
  53. }
  54. public virtual void CancelEdit()
  55. {
  56. if (IsListenEditing)
  57. {
  58. IsEdited = m_TempEdit;
  59. }
  60. }
  61. public virtual void EndEdit()
  62. {
  63. if (IsListenEditing&&!IsEdited)
  64. {
  65. this.IsEdited = true;
  66. }
  67. }
  68. #endregion
  69. /// <summary>
  70. /// 在集合中标志元素是否是新添加的
  71. /// </summary>
  72. public bool NewAdd { get; set; }
  73. /// <summary>
  74. /// 数据是否经过了编辑,状态仅由内部维护【当前类型数据,基础类元素,不包括关联类型元素的编辑状态】
  75. /// </summary>
  76. public bool IsEdited { get;private set; }
  77. /// <summary>
  78. /// 是否监控数据的编辑状态
  79. /// </summary>
  80. public bool IsListenEditing { get; set; }
  81. /// <summary>
  82. /// 设置数据是否处于编辑状态
  83. /// </summary>
  84. /// <param name="isEdited"></param>
  85. public void SetEditedState(bool isEdited)
  86. {
  87. this.IsEdited = IsEdited;
  88. }
  89. /// <summary>
  90. /// 是否处于编辑状态【包括关联的对象的编辑状态】
  91. /// </summary>
  92. /// <returns></returns>
  93. public virtual bool GetEditedState()
  94. {
  95. if (IsEdited)
  96. return true;
  97. var currentType = this.GetType();
  98. var properties = currentType.GetProperties();
  99. foreach (var propertyInfo in properties)
  100. {
  101. if (propertyInfo.PropertyType != typeof(EditableItem))
  102. continue;
  103. EditableItem edit = propertyInfo.GetValue(this) as EditableItem;
  104. if (edit != null && edit.GetEditedState())
  105. return true;
  106. }
  107. return false;
  108. }
  109. }
  110. }