99 lines
3.7 KiB
C#
99 lines
3.7 KiB
C#
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Data;
|
|
|
|
namespace Cowain.Base.Helpers;
|
|
|
|
public class GridDefinitionsHelper
|
|
{
|
|
// 定义Rows附加属性
|
|
public static readonly AttachedProperty<int> RowsProperty =
|
|
AvaloniaProperty.RegisterAttached<GridDefinitionsHelper, Grid, int>("Rows", 0);
|
|
|
|
// 定义Columns附加属性
|
|
public static readonly AttachedProperty<int> ColumnsProperty =
|
|
AvaloniaProperty.RegisterAttached<GridDefinitionsHelper, Grid, int>("Columns", 0);
|
|
|
|
// 定义RowHeight附加属性
|
|
public static readonly AttachedProperty<string> RowHeightProperty =
|
|
AvaloniaProperty.RegisterAttached<GridDefinitionsHelper, Grid, string>("RowHeight", "Auto");
|
|
|
|
// 定义ColumnWidth附加属性
|
|
public static readonly AttachedProperty<string> ColumnWidthProperty =
|
|
AvaloniaProperty.RegisterAttached<GridDefinitionsHelper, Grid, string>("ColumnWidth", "Auto");
|
|
|
|
// Getter和Setter方法
|
|
public static int GetRows(AvaloniaObject element) => element.GetValue(RowsProperty);
|
|
public static void SetRows(AvaloniaObject element, int value) => element.SetValue(RowsProperty, value);
|
|
|
|
public static int GetColumns(AvaloniaObject element) => element.GetValue(ColumnsProperty);
|
|
public static void SetColumns(AvaloniaObject element, int value) => element.SetValue(ColumnsProperty, value);
|
|
|
|
public static string GetRowHeight(AvaloniaObject element) => element.GetValue(RowHeightProperty);
|
|
public static void SetRowHeight(AvaloniaObject element, string value) => element.SetValue(RowHeightProperty, value);
|
|
|
|
public static string GetColumnWidth(AvaloniaObject element) => element.GetValue(ColumnWidthProperty);
|
|
public static void SetColumnWidth(AvaloniaObject element, string value) => element.SetValue(ColumnWidthProperty, value);
|
|
|
|
static GridDefinitionsHelper()
|
|
{
|
|
// 监听Rows属性变化
|
|
RowsProperty.Changed.AddClassHandler<Grid>((grid, e) => UpdateGridDefinitions(grid));
|
|
|
|
// 监听Columns属性变化
|
|
ColumnsProperty.Changed.AddClassHandler<Grid>((grid, e) => UpdateGridDefinitions(grid));
|
|
|
|
// 监听RowHeight属性变化
|
|
RowHeightProperty.Changed.AddClassHandler<Grid>((grid, e) => UpdateGridDefinitions(grid));
|
|
|
|
// 监听ColumnWidth属性变化
|
|
ColumnWidthProperty.Changed.AddClassHandler<Grid>((grid, e) => UpdateGridDefinitions(grid));
|
|
}
|
|
|
|
private static void UpdateGridDefinitions(Grid grid)
|
|
{
|
|
int rows = GetRows(grid);
|
|
int columns = GetColumns(grid);
|
|
string rowHeight = GetRowHeight(grid);
|
|
string columnWidth = GetColumnWidth(grid);
|
|
|
|
// 更新行定义
|
|
grid.RowDefinitions.Clear();
|
|
var height = ParseDimension(rowHeight);
|
|
for (int i = 0; i < rows; i++)
|
|
{
|
|
grid.RowDefinitions.Add(new RowDefinition(height));
|
|
}
|
|
|
|
// 更新列定义
|
|
grid.ColumnDefinitions.Clear();
|
|
var width = ParseDimension(columnWidth);
|
|
for (int i = 0; i < columns; i++)
|
|
{
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition(width));
|
|
}
|
|
}
|
|
|
|
private static GridLength ParseDimension(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
return GridLength.Auto;
|
|
|
|
value = value.Trim();
|
|
if (value.EndsWith("*"))
|
|
{
|
|
if (value.Length == 1)
|
|
return new GridLength(1, GridUnitType.Star);
|
|
|
|
if (double.TryParse(value.Substring(0, value.Length - 1), out double starValue))
|
|
return new GridLength(starValue, GridUnitType.Star);
|
|
}
|
|
else if (double.TryParse(value, out double pixelValue))
|
|
{
|
|
return new GridLength(pixelValue, GridUnitType.Pixel);
|
|
}
|
|
|
|
return GridLength.Auto;
|
|
}
|
|
}
|