123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- namespace FWindSoft.Wpf
- {
- public class EditableItem : INotifyPropertyChanged,IEditableObject
- {
- public EditableItem()
- {
- IsEdited = false;
- NewAdd = false;
- IsListenEditing = false;
- }
- #region 实现INotifyPropertyChanged
- public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
-
- protected virtual void RaisePropertyChanged(String propertyName = "")
- {
- if (IsListenEditing)
- {
- this.IsEdited = true;
- }
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
- {
- if (null != propertyExpression)
- {
- var memberExpression = propertyExpression.Body as MemberExpression;
- if (memberExpression != null)
- RaisePropertyChanged(memberExpression.Member.Name);
- }
- else
- {
- throw new ArgumentNullException("propertyExpression");
- }
- }
- #endregion
- #region 实现EditableObject,UI驱动数据
- private bool m_TempEdit;
- public virtual void BeginEdit()
- {
- if (IsListenEditing)
- {
- m_TempEdit=IsEdited;
- //IsEdited = true;
- }
- }
- public virtual void CancelEdit()
- {
- if (IsListenEditing)
- {
- IsEdited = m_TempEdit;
- }
- }
- public virtual void EndEdit()
- {
- if (IsListenEditing&&!IsEdited)
- {
- this.IsEdited = true;
- }
- }
- #endregion
- /// <summary>
- /// 在集合中标志元素是否是新添加的
- /// </summary>
- public bool NewAdd { get; set; }
- /// <summary>
- /// 数据是否经过了编辑,状态仅由内部维护【当前类型数据,基础类元素,不包括关联类型元素的编辑状态】
- /// </summary>
- public bool IsEdited { get;private set; }
- /// <summary>
- /// 是否监控数据的编辑状态
- /// </summary>
- public bool IsListenEditing { get; set; }
- /// <summary>
- /// 设置数据是否处于编辑状态
- /// </summary>
- /// <param name="isEdited"></param>
- public void SetEditedState(bool isEdited)
- {
- this.IsEdited = IsEdited;
- }
- /// <summary>
- /// 是否处于编辑状态【包括关联的对象的编辑状态】
- /// </summary>
- /// <returns></returns>
- public virtual bool GetEditedState()
- {
- if (IsEdited)
- return true;
- var currentType = this.GetType();
- var properties = currentType.GetProperties();
- foreach (var propertyInfo in properties)
- {
- if (propertyInfo.PropertyType != typeof(EditableItem))
- continue;
- EditableItem edit = propertyInfo.GetValue(this) as EditableItem;
- if (edit != null && edit.GetEditedState())
- return true;
- }
- return false;
- }
- }
- }
|