DynamicItemsListGrid.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. namespace InABox.DynamicGrid;
  12. public interface IDynamicItemsListGrid : IDynamicGrid
  13. {
  14. /// <summary>
  15. /// The items list that forms the source for the rows of this grid
  16. /// </summary>
  17. /// <remarks>
  18. /// <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.
  19. /// </remarks>
  20. IList Items { get; set; }
  21. }
  22. public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
  23. where T : BaseObject, new()
  24. {
  25. private List<T> _items = [];
  26. public List<T> Items
  27. {
  28. get => _items;
  29. set => _items = value;
  30. }
  31. IList IDynamicItemsListGrid.Items
  32. {
  33. get => _items;
  34. set => _items = value as List<T> ?? new List<T>();
  35. }
  36. protected override void Init()
  37. {
  38. }
  39. protected override void DoReconfigure(DynamicGridOptions options)
  40. {
  41. }
  42. public override void DeleteItems(params CoreRow[] rows)
  43. {
  44. foreach (var row in rows.OrderByDescending(x => x.Index))
  45. {
  46. Items.RemoveAt(_recordmap[row].Index);
  47. }
  48. }
  49. public override T LoadItem(CoreRow row)
  50. {
  51. return Items[_recordmap[row].Index];
  52. }
  53. protected override void Reload(
  54. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  55. CancellationToken token, Action<CoreTable?, Exception?> action)
  56. {
  57. var result = new CoreTable();
  58. result.LoadColumns(columns);
  59. result.LoadRows(Items);
  60. action.Invoke(result, null);
  61. }
  62. public override void SaveItem(T item)
  63. {
  64. if (!Items.Contains(item))
  65. {
  66. Items.Add(item);
  67. }
  68. }
  69. }