using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace Lattice.Layout.UI.WinUI.Controls; /// /// Контейнер для отображения сплит-элемента раскладки. /// Использует Grid и автоматически создаёт строки/столбцы под детей. /// public sealed class WinUISplitControl : Grid { /// /// Ориентация сплита (горизонтальная или вертикальная). /// 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(); } /// /// Перестраивает структуру Grid в зависимости от ориентации и количества детей. /// 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); } } } /// /// Добавляет дочерний элемент и перестраивает Grid. /// public new void ChildrenChanged() { RebuildGrid(); } }