DynamicItemsListGrid.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using InABox.Core;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Microsoft.CodeAnalysis.CSharp.Syntax;
  9. using Syncfusion.Windows.Tools.Controls;
  10. using System.Threading;
  11. using System.Windows;
  12. namespace InABox.DynamicGrid;
  13. public interface IDynamicItemsListGrid : IDynamicGrid
  14. {
  15. /// <summary>
  16. /// The items list that forms the source for the rows of this grid
  17. /// </summary>
  18. /// <remarks>
  19. /// <b>Note:</b> This must be a list of type <see cref="List{T}"/>, otherwise the assignment to this property <u>will not</u> work.
  20. /// </remarks>
  21. IList Items { get; set; }
  22. }
  23. public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
  24. where T : BaseObject, new()
  25. {
  26. private List<T> _items = [];
  27. public List<T> Items
  28. {
  29. get => _items;
  30. set => _items = value;
  31. }
  32. IList IDynamicItemsListGrid.Items
  33. {
  34. get => _items;
  35. set => _items = value as List<T> ?? new List<T>();
  36. }
  37. public override void OnItemSourceChanged(object value)
  38. {
  39. if (value is List<T> list)
  40. {
  41. Items = list;
  42. Refresh(true,true);
  43. }
  44. }
  45. protected override void Init()
  46. {
  47. }
  48. protected override void DoReconfigure(DynamicGridOptions options)
  49. {
  50. }
  51. public override void DeleteItems(params CoreRow[] rows)
  52. {
  53. foreach (var row in rows.OrderByDescending(x => x.Index))
  54. {
  55. Items.RemoveAt(_recordmap[row].Index);
  56. }
  57. }
  58. public override T LoadItem(CoreRow row)
  59. {
  60. return Items[_recordmap[row].Index];
  61. }
  62. protected override void Reload(
  63. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  64. CancellationToken token, Action<CoreTable?, Exception?> action)
  65. {
  66. var result = new CoreTable();
  67. result.LoadColumns(columns);
  68. result.LoadRows(Items);
  69. action.Invoke(result, null);
  70. }
  71. public override void SaveItem(T item)
  72. {
  73. if (!Items.Contains(item))
  74. {
  75. Items.Add(item);
  76. }
  77. if (item is ISequenceable)
  78. {
  79. Items.Sort((a, b) => (a as ISequenceable)!.Sequence.CompareTo((b as ISequenceable)!.Sequence));
  80. }
  81. }
  82. protected override bool BeforeCopy(IList<T> items)
  83. {
  84. if (!base.BeforeCopy(items)) return false;
  85. for(int i = 0; i < items.Count; ++i)
  86. {
  87. items[i] = items[i].Clone();
  88. }
  89. return true;
  90. }
  91. }