DataGridCellCollection.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. namespace Avalonia.Controls
  10. {
  11. internal class DataGridCellCollection
  12. {
  13. private List<DataGridCell> _cells;
  14. private DataGridRow _owningRow;
  15. internal event EventHandler<DataGridCellEventArgs> CellAdded;
  16. internal event EventHandler<DataGridCellEventArgs> CellRemoved;
  17. public DataGridCellCollection(DataGridRow owningRow)
  18. {
  19. _owningRow = owningRow;
  20. _cells = new List<DataGridCell>();
  21. }
  22. public int Count
  23. {
  24. get
  25. {
  26. return _cells.Count;
  27. }
  28. }
  29. public IEnumerator GetEnumerator()
  30. {
  31. return _cells.GetEnumerator();
  32. }
  33. public void Insert(int cellIndex, DataGridCell cell)
  34. {
  35. Debug.Assert(cellIndex >= 0 && cellIndex <= _cells.Count);
  36. Debug.Assert(cell != null);
  37. cell.OwningRow = _owningRow;
  38. _cells.Insert(cellIndex, cell);
  39. CellAdded?.Invoke(this, new DataGridCellEventArgs(cell));
  40. }
  41. public void RemoveAt(int cellIndex)
  42. {
  43. DataGridCell dataGridCell = _cells[cellIndex];
  44. _cells.RemoveAt(cellIndex);
  45. dataGridCell.OwningRow = null;
  46. CellRemoved?.Invoke(this, new DataGridCellEventArgs(dataGridCell));
  47. }
  48. public DataGridCell this[int index]
  49. {
  50. get
  51. {
  52. if (index < 0 || index >= _cells.Count)
  53. {
  54. throw DataGridError.DataGrid.ValueMustBeBetween("index", "Index", 0, true, _cells.Count, false);
  55. }
  56. return _cells[index];
  57. }
  58. }
  59. }
  60. }