Files
GSS2Rework/GSS2.UI.Core/Behaviors/GridColumnHideBehavior.cs
T

43 lines
1.7 KiB
C#

// Оригинальное решение для проблемы сворачивания колонок в Grid от
// https://github.com/AvaloniaUI/Avalonia/discussions/6773#discussioncomment-1514604
using Avalonia;
using Avalonia.Controls;
namespace GSS2.UI.Core.Behaviors;
public static class GridColumnHideBehavior
{
private readonly static GridLength ZeroWidth = new GridLength(0, GridUnitType.Pixel);
private readonly static AttachedProperty<GridLength?> LastWidthProperty =
AvaloniaProperty.RegisterAttached<ColumnDefinition, GridLength?>("LastWidth", typeof(GridColumnHideBehavior), default);
public readonly static AttachedProperty<bool> IsVisibleProperty =
AvaloniaProperty.RegisterAttached<ColumnDefinition, bool>("IsVisible", typeof(GridColumnHideBehavior), true,
coerce: (element, visibility) =>
{
var lastWidth = element.GetValue(LastWidthProperty);
if (visibility && lastWidth is { })
element.SetValue(ColumnDefinition.WidthProperty, lastWidth);
else if (!visibility)
{
element.SetValue(LastWidthProperty, element.GetValue(ColumnDefinition.WidthProperty));
element.SetValue(ColumnDefinition.WidthProperty, ZeroWidth);
}
return visibility;
}
);
public static bool GetIsVisible(ColumnDefinition columnDefinition)
{
return columnDefinition.GetValue(IsVisibleProperty);
}
public static void SetIsVisible(ColumnDefinition columnDefinition, bool visibility)
{
columnDefinition.SetValue(IsVisibleProperty, visibility);
}
}