| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 | using Avalonia;using Avalonia.Controls;using Avalonia.LogicalTree;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InABox.Avalonia;public static class AvaloniaUtils{    #region Grid Children    public static int GetRow(this Grid grid, ILogical dependencyObject)    {        while (true)        {            var parent = dependencyObject.GetLogicalParent();            if (parent == null)                return -1;            if (parent == grid)                return dependencyObject is Control control ? Grid.GetRow(control) : -1;            dependencyObject = parent;        }    }    public static int GetRowSpan(this Grid grid, ILogical dependencyObject)    {        while (true)        {            var parent = dependencyObject.GetLogicalParent();            if (parent == null)                return -1;            if (parent == grid)                return dependencyObject is Control control ? Grid.GetRowSpan(control) : -1;            dependencyObject = parent;        }    }       public static int GetColumn(this Grid grid, ILogical dependencyObject)    {        while (true)        {            var parent = dependencyObject.GetLogicalParent();            if (parent == null)                return -1;            if (parent == grid)                return dependencyObject is Control control ? Grid.GetColumn(control) : -1;            dependencyObject = parent;        }    }    public static int GetColumnSpan(this Grid grid, ILogical dependencyObject)    {        while (true)        {            var parent = dependencyObject.GetLogicalParent();            if (parent == null)                return -1;            if (parent == grid)                return dependencyObject is Control control ? Grid.GetColumnSpan(control) : -1;            dependencyObject = parent;        }    }       public static void SetGridPosition(this AvaloniaObject element, int row, int column, int rowspan = 1, int colspan = 1)    {        element.SetValue(Grid.ColumnProperty, column);        element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));        element.SetValue(Grid.RowProperty, row);        element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));    }    public static Grid AddChild(this Grid grid, Control element, int row, int column, int rowSpan = 1, int colSpan = 1)    {        element.SetGridPosition(row, column, rowSpan, colSpan);        grid.Children.Add(element);        return grid;    }    #endregion    #region Grid Columns + Rows    public static ColumnDefinition AddColumn(this Grid grid, GridUnitType type, double value = 1)    {        var colDef = new ColumnDefinition { Width = new GridLength(value, type) };        grid.ColumnDefinitions.Add(colDef);        return colDef;    }    public static ColumnDefinition AddColumn(this Grid grid, double value)    {        var colDef = new ColumnDefinition { Width = new GridLength(value) };        grid.ColumnDefinitions.Add(colDef);        return colDef;    }    public static RowDefinition AddRow(this Grid grid, GridUnitType type, double value = 1)    {        var rowDef = new RowDefinition { Height = new GridLength(value, type) };        grid.RowDefinitions.Add(rowDef);        return rowDef;    }    public static RowDefinition AddRow(this Grid grid, double value)    {        var rowDef = new RowDefinition { Height = new GridLength(value) };        grid.RowDefinitions.Add(rowDef);        return rowDef;    }    #endregion}
 |