BindingData.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. /// <summary>
  13. /// <para>一个更高性能的用于数据绑定的对象,继承自BindingList对象</para>
  14. /// <para>添加了AddRange、InsertRange,支持批量添加后再整体刷新视图,提高性能</para>
  15. /// <para>添加了几个常用的类似Linq中List的函数</para>
  16. /// <para>该对象可直接用于json序列化和反序列化,其结果和List一样</para>
  17. /// </summary>
  18. /// <typeparam name="T">泛型对象</typeparam>
  19. /// <remarks>BindingData不具有线程安全特性,该对象应该只初始化一次,通过Clear、Add等更新</remarks>
  20. public class BindingData<T> : BindingList<T> where T : class, new()
  21. {
  22. public event Action<T> OnItemDelete;
  23. public BindingData()
  24. : base()
  25. {
  26. }
  27. /// <summary>
  28. /// 是否支持排序
  29. /// </summary>
  30. protected override bool SupportsSortingCore
  31. {
  32. get { return true; }
  33. }
  34. /// <summary>
  35. /// 是否支持搜索
  36. /// </summary>
  37. protected override bool SupportsSearchingCore
  38. {
  39. get { return true; }
  40. }
  41. protected override void RemoveItem(int itemIndex)
  42. {
  43. OnItemDelete?.Invoke(this[itemIndex]);
  44. base.RemoveItem(itemIndex);
  45. }
  46. public BindingData(IEnumerable<T> data)
  47. {
  48. //构造函数中不会触发OnListChanged
  49. this.AddRange(data);
  50. }
  51. public void AddRange(IEnumerable<T> items)
  52. {
  53. if (items == null || items.Count() == 0) return;
  54. RaiseListChangedEvents = false;//让View不刷新
  55. foreach (var item in items)
  56. {
  57. Add(item);
  58. }
  59. OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));//批量更新View
  60. RaiseListChangedEvents = true;//让View继续更新
  61. }
  62. public void InsertRange(IEnumerable<T> items)
  63. {
  64. if (items == null || items.Count() == 0) return;
  65. RaiseListChangedEvents = false;//让View不刷新
  66. int count = items.Count();
  67. for (int i = count - 1; i >= 0; i--)
  68. {
  69. InsertItem(0, items.ElementAt(i));
  70. }
  71. OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));//批量更新View
  72. RaiseListChangedEvents = true;//让View继续更新
  73. }
  74. public T[] GetRows(int[] rows)
  75. {
  76. if (this.Count < 0) return null;
  77. List<T> list = new List<T>();
  78. foreach (var index in rows)
  79. {
  80. if (index < 0 || index >= this.Count) continue;
  81. list.Add(this[index]);
  82. }
  83. return list.ToArray();
  84. }
  85. public int RemoveAll(Predicate<T> match)
  86. {
  87. var list = this.FindAll(match);
  88. RaiseListChangedEvents = false;//让View不刷新
  89. int i = 0;
  90. foreach (var item in list)
  91. {
  92. if (this.Remove(item)) i++;
  93. }
  94. OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));//批量更新View
  95. RaiseListChangedEvents = true;//让View继续更新
  96. return i;
  97. }
  98. public void RemoveAll(T[] data)
  99. {
  100. foreach (var item in data)
  101. {
  102. this.Remove(item);
  103. }
  104. }
  105. public T Find(Predicate<T> match)
  106. {
  107. foreach (var item in this)
  108. {
  109. if (match(item))
  110. return item;
  111. }
  112. return default;
  113. }
  114. public List<T> FindAll(Predicate<T> match)
  115. {
  116. List<T> list = new List<T>();
  117. foreach (var item in this)
  118. {
  119. if (match(item))
  120. list.Add(item);
  121. }
  122. return list;
  123. }
  124. public int FindIndex(Predicate<T> match)
  125. {
  126. int idx = 0;
  127. foreach (var item in this)
  128. {
  129. if (match(item))
  130. return idx;
  131. idx++;
  132. }
  133. return -1;
  134. }
  135. public bool Exists(Predicate<T> match)
  136. {
  137. foreach (var item in this)
  138. {
  139. if (match(item))
  140. return true;
  141. }
  142. return false;
  143. }
  144. public void SortData(Expression<Func<T, dynamic>> express, bool asc = true)
  145. {
  146. RaiseListChangedEvents = false;//让View不刷新
  147. List<T> tmp;
  148. if (asc)
  149. tmp = this.OrderBy(express.Compile()).ToList();
  150. else
  151. tmp = this.OrderByDescending(express.Compile()).ToList();
  152. this.Clear();
  153. this.AddRange(tmp);
  154. RaiseListChangedEvents = true;//让View继续更新
  155. }
  156. public List<T> DeepClone()
  157. {
  158. ParameterExpression parameterExpression = Expression.Parameter(typeof(T), "p");
  159. List<MemberBinding> memberBindingList = new List<MemberBinding>();
  160. foreach (var item in typeof(T).GetProperties())
  161. {
  162. if (!item.CanWrite)
  163. continue;
  164. MemberExpression property = Expression.Property(parameterExpression, typeof(T).GetProperty(item.Name));
  165. MemberBinding memberBinding = Expression.Bind(item, property);
  166. memberBindingList.Add(memberBinding);
  167. }
  168. MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(T)), memberBindingList.ToArray());
  169. Expression<Func<T, T>> lambda = Expression.Lambda<Func<T, T>>(memberInitExpression, new ParameterExpression[] { parameterExpression });
  170. List<T> list = new List<T>();
  171. foreach (var item in this)
  172. {
  173. list.Add(lambda.Compile()(item));
  174. }
  175. return list;
  176. }
  177. }