86 lines
2.7 KiB
C#
86 lines
2.7 KiB
C#
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
namespace Lattice.Layout.UI.WinUI.Controls;
|
|
|
|
/// <summary>
|
|
/// Контейнер для отображения сплит-элемента раскладки.
|
|
/// Использует Grid и автоматически создаёт строки/столбцы под детей.
|
|
/// </summary>
|
|
public sealed class WinUISplitControl : Grid
|
|
{
|
|
/// <summary>
|
|
/// Ориентация сплита (горизонтальная или вертикальная).
|
|
/// </summary>
|
|
public Orientation LayoutOrientation
|
|
{
|
|
get => (Orientation)GetValue(LayoutOrientationProperty);
|
|
set => SetValue(LayoutOrientationProperty, value);
|
|
}
|
|
|
|
public static readonly DependencyProperty LayoutOrientationProperty =
|
|
DependencyProperty.Register(
|
|
nameof(LayoutOrientation),
|
|
typeof(Orientation),
|
|
typeof(WinUISplitControl),
|
|
new PropertyMetadata(Orientation.Horizontal, OnOrientationChanged));
|
|
|
|
private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (d is WinUISplitControl control)
|
|
control.RebuildGrid();
|
|
}
|
|
|
|
public WinUISplitControl()
|
|
{
|
|
Loaded += (_, _) => RebuildGrid();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Перестраивает структуру Grid в зависимости от ориентации и количества детей.
|
|
/// </summary>
|
|
public void RebuildGrid()
|
|
{
|
|
RowDefinitions.Clear();
|
|
ColumnDefinitions.Clear();
|
|
|
|
if (Children.Count == 0)
|
|
return;
|
|
|
|
if (LayoutOrientation == Orientation.Horizontal)
|
|
{
|
|
// Горизонтальный сплит → столбцы
|
|
for (int i = 0; i < Children.Count; i++)
|
|
{
|
|
ColumnDefinitions.Add(new ColumnDefinition
|
|
{
|
|
Width = new GridLength(1, GridUnitType.Star)
|
|
});
|
|
|
|
if (Children[i] is FrameworkElement fe) Grid.SetColumn(fe, i);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Вертикальный сплит → строки
|
|
for (int i = 0; i < Children.Count; i++)
|
|
{
|
|
RowDefinitions.Add(new RowDefinition
|
|
{
|
|
Height = new GridLength(1, GridUnitType.Star)
|
|
});
|
|
|
|
if (Children[i] is FrameworkElement fe) Grid.SetColumn(fe, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавляет дочерний элемент и перестраивает Grid.
|
|
/// </summary>
|
|
public new void ChildrenChanged()
|
|
{
|
|
RebuildGrid();
|
|
}
|
|
}
|