using Avalonia; using Avalonia.Controls; using System.Diagnostics; namespace Plugin.Cowain.Wcs.Controls; public class CustomGridPanel : Panel { // 定义Row附加属性 public static readonly AttachedProperty RowProperty = AvaloniaProperty.RegisterAttached( "Row", 0); // 定义Column附加属性 public static readonly AttachedProperty ColumnProperty = AvaloniaProperty.RegisterAttached( "Column", 0); // 定义CellSize附加属性 public static readonly AttachedProperty CellSizeProperty = AvaloniaProperty.RegisterAttached( "CellSize", new Size(100, 100)); // 属性访问器 public static int GetRow(Control element) => element.GetValue(RowProperty); public static void SetRow(Control element, int value) => element.SetValue(RowProperty, value); public static int GetColumn(Control element) => element.GetValue(ColumnProperty); public static void SetColumn(Control element, int value) => element.SetValue(ColumnProperty, value); public static Size GetCellSize(Control element) => element.GetValue(CellSizeProperty); public static void SetCellSize(Control element, Size value) => element.SetValue(CellSizeProperty, value); protected override Size MeasureOverride(Size availableSize) { Debug.WriteLine("=== Measuring CustomGridPanel ==="); int maxRow = 0; int maxColumn = 0; foreach (var child in Children) { int row = GetRow(child); int column = GetColumn(child); Debug.WriteLine($"Child: {child.GetType().Name}, Row: {row}, Column: {column}"); maxRow = Math.Max(maxRow, row); maxColumn = Math.Max(maxColumn, column); child.Measure(availableSize); } var cellSize = GetCellSize(this); var result = new Size((maxColumn + 1) * cellSize.Width, (maxRow + 1) * cellSize.Height); Debug.WriteLine($"Measured size: {result.Width} x {result.Height}"); return result; } protected override Size ArrangeOverride(Size finalSize) { Debug.WriteLine("=== Arranging CustomGridPanel ==="); var cellSize = GetCellSize(this); foreach (var child in Children) { int row = GetRow(child); int column = GetColumn(child); Debug.WriteLine($"Arranging child at Row: {row}, Column: {column}"); Rect rect = new Rect( column * cellSize.Width, row * cellSize.Height, cellSize.Width, cellSize.Height); child.Arrange(rect); } return finalSize; } }