InnerCheckItem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Reflection;
  5. using FWindSoft.Data;
  6. namespace FWindSoft.Wpf
  7. {
  8. /*
  9. * 点击select选中项操作:true,该选中项以下的项目,全选;该项目以上的选项 全选
  10. * false,该选中项以下的项目,全不选;该项目以上的项目重新检测,是否选中,如果出现一个选中则 在往上全选
  11. */
  12. /// <summary>
  13. /// 内部使用勾选类
  14. /// </summary>
  15. internal class InnerCheckItem : BasePropertyChanged
  16. {
  17. private bool m_IsChecked;
  18. private string m_Display;
  19. private object m_RefObject;
  20. public InnerCheckItem(object o,string namePath)
  21. {
  22. this.m_RefObject = o;
  23. if (namePath == null)
  24. {
  25. this.Display = o.ToString();
  26. return;
  27. }
  28. PropertyInfo propertyInfo = o.GetType().GetProperty(namePath);
  29. if (propertyInfo == null)
  30. {
  31. this.Display = o.ToString();
  32. return;
  33. // throw new Exception("绑定类型必须包含Name属性");
  34. }
  35. this.Display = propertyInfo.GetValue(o,null).ToString();
  36. }
  37. #region 属性
  38. /// <summary>
  39. /// 是否选中
  40. /// </summary>
  41. public bool IsChecked
  42. {
  43. get { return this.m_IsChecked; }
  44. set
  45. {
  46. this.m_IsChecked = value;
  47. RaisePropertyChanged("IsChecked");
  48. }
  49. }
  50. /// <summary>
  51. /// 标签显示
  52. /// </summary>
  53. public string Display
  54. {
  55. get { return this.m_Display; }
  56. set
  57. {
  58. this.m_Display = value;
  59. RaisePropertyChanged("IsDisplay");
  60. }
  61. }
  62. public object RefObject
  63. {
  64. get { return this.m_RefObject; }
  65. }
  66. public InnerCheckItemCollection Prarent { get; set; }
  67. #endregion
  68. public virtual void CheckedChanged()
  69. {
  70. ValidateParentChecked(this.Prarent);
  71. }
  72. private void ValidateParentChecked(InnerCheckItemCollection parent)
  73. {
  74. if (parent == null)
  75. return;
  76. parent.IsChecked=parent.AnyChildChecked();
  77. ValidateParentChecked(parent.Prarent);
  78. }
  79. }
  80. /// <summary>
  81. /// 内部使用勾选类集合
  82. /// </summary>
  83. internal class InnerCheckItemCollection : InnerCheckItem
  84. {
  85. #region 集合类扩展集合属性
  86. private ObservableCollection<InnerCheckItem> m_Items;
  87. public ObservableCollection<InnerCheckItem> Items
  88. {
  89. get { return m_Items; }
  90. }
  91. public void AddItem(InnerCheckItem item)
  92. {
  93. item.Prarent = this;
  94. this.Items.Add(item);
  95. }
  96. #endregion
  97. public InnerCheckItemCollection(object o, string namePath)
  98. : base(o, namePath)
  99. {
  100. m_Items=new ObservableCollection<InnerCheckItem>();
  101. }
  102. #region 选中逻辑处理
  103. /*
  104. * 父元素回溯一步一步检测递归
  105. * 子元素集合迭代复制处理
  106. *
  107. */
  108. public override void CheckedChanged()
  109. {
  110. bool currentChecked=this.IsChecked;
  111. //迭代处理子元素
  112. List<InnerCheckItem> tempItems=new List<InnerCheckItem>();
  113. tempItems.AddRange(this.Items);
  114. for (int i = 0; i < tempItems.Count; i++)
  115. {
  116. InnerCheckItem tempItem = tempItems[i];
  117. tempItem.IsChecked = currentChecked;
  118. InnerCheckItemCollection tempCollection = tempItem as InnerCheckItemCollection;
  119. if (tempCollection != null)
  120. {
  121. tempItems.AddRange(tempCollection.Items);
  122. }
  123. }
  124. base.CheckedChanged();
  125. }
  126. public bool AnyChildChecked()
  127. {
  128. return this.Items.Any(t => t.IsChecked);
  129. }
  130. #endregion
  131. }
  132. }