using System.Collections.Generic; using System.Linq; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Layout; using Avalonia.Styling; using HandyControl.Data; namespace HandyControl.Controls; public class ButtonGroup : ItemsControl { public static readonly StyledProperty OrientationProperty = StackPanel.OrientationProperty.AddOwner( new StyledPropertyMetadata(defaultValue: Orientation.Horizontal)); public Orientation Orientation { get => GetValue(OrientationProperty); set => SetValue(OrientationProperty, value); } public static readonly StyledProperty LayoutProperty = AvaloniaProperty.Register(nameof(Layout), defaultValue: LinearLayout.Uniform); public LinearLayout Layout { get => GetValue(LayoutProperty); set => SetValue(LayoutProperty, value); } static ButtonGroup() { AffectsMeasure(OrientationProperty, LayoutProperty); } protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) { if (item is Button or RadioButton or ToggleButton) { recycleKey = null; return false; } return base.NeedsContainerOverride(item, index, out recycleKey); } protected override void PrepareContainerForItemOverride(Control container, object? item, int index) { base.PrepareContainerForItemOverride(container, item, index); container.PropertyChanged += Container_PropertyChanged; UpdateItemThemes(); } protected override void ClearContainerForItemOverride(Control container) { base.ClearContainerForItemOverride(container); container.PropertyChanged -= Container_PropertyChanged; UpdateItemThemes(); } private void Container_PropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) { if (e.Property == IsVisibleProperty) { UpdateItemThemes(); } } private void UpdateItemThemes() { var visibleButtons = Items.OfType