AvaloniaUtils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.LogicalTree;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace InABox.Avalonia;
  10. public static class AvaloniaUtils
  11. {
  12. #region Grid Children
  13. public static int GetRow(this Grid grid, ILogical dependencyObject)
  14. {
  15. while (true)
  16. {
  17. var parent = dependencyObject.GetLogicalParent();
  18. if (parent == null)
  19. return -1;
  20. if (parent == grid)
  21. return dependencyObject is Control control ? Grid.GetRow(control) : -1;
  22. dependencyObject = parent;
  23. }
  24. }
  25. public static int GetRowSpan(this Grid grid, ILogical dependencyObject)
  26. {
  27. while (true)
  28. {
  29. var parent = dependencyObject.GetLogicalParent();
  30. if (parent == null)
  31. return -1;
  32. if (parent == grid)
  33. return dependencyObject is Control control ? Grid.GetRowSpan(control) : -1;
  34. dependencyObject = parent;
  35. }
  36. }
  37. public static int GetColumn(this Grid grid, ILogical dependencyObject)
  38. {
  39. while (true)
  40. {
  41. var parent = dependencyObject.GetLogicalParent();
  42. if (parent == null)
  43. return -1;
  44. if (parent == grid)
  45. return dependencyObject is Control control ? Grid.GetColumn(control) : -1;
  46. dependencyObject = parent;
  47. }
  48. }
  49. public static int GetColumnSpan(this Grid grid, ILogical dependencyObject)
  50. {
  51. while (true)
  52. {
  53. var parent = dependencyObject.GetLogicalParent();
  54. if (parent == null)
  55. return -1;
  56. if (parent == grid)
  57. return dependencyObject is Control control ? Grid.GetColumnSpan(control) : -1;
  58. dependencyObject = parent;
  59. }
  60. }
  61. public static void SetGridPosition(this AvaloniaObject element, int row, int column, int rowspan = 1, int colspan = 1)
  62. {
  63. element.SetValue(Grid.ColumnProperty, column);
  64. element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
  65. element.SetValue(Grid.RowProperty, row);
  66. element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
  67. }
  68. public static Grid AddChild(this Grid grid, Control element, int row, int column, int rowSpan = 1, int colSpan = 1)
  69. {
  70. element.SetGridPosition(row, column, rowSpan, colSpan);
  71. grid.Children.Add(element);
  72. return grid;
  73. }
  74. #endregion
  75. #region Grid Columns + Rows
  76. public static ColumnDefinition AddColumn(this Grid grid, GridUnitType type, double value = 1)
  77. {
  78. var colDef = new ColumnDefinition { Width = new GridLength(value, type) };
  79. grid.ColumnDefinitions.Add(colDef);
  80. return colDef;
  81. }
  82. public static ColumnDefinition AddColumn(this Grid grid, double value)
  83. {
  84. var colDef = new ColumnDefinition { Width = new GridLength(value) };
  85. grid.ColumnDefinitions.Add(colDef);
  86. return colDef;
  87. }
  88. public static RowDefinition AddRow(this Grid grid, GridUnitType type, double value = 1)
  89. {
  90. var rowDef = new RowDefinition { Height = new GridLength(value, type) };
  91. grid.RowDefinitions.Add(rowDef);
  92. return rowDef;
  93. }
  94. public static RowDefinition AddRow(this Grid grid, double value)
  95. {
  96. var rowDef = new RowDefinition { Height = new GridLength(value) };
  97. grid.RowDefinitions.Add(rowDef);
  98. return rowDef;
  99. }
  100. #endregion
  101. }