DynamicItemsListGrid.cs 1.9 KB

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