using System; using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; using Avalonia.Styling; namespace Avalonia.Controls { /// /// A button with primary and secondary parts that can each be pressed separately. /// The primary part behaves like a with two states and /// the secondary part opens a flyout. /// [PseudoClasses(pcChecked)] public class ToggleSplitButton : SplitButton, IStyleable { /// /// Raised when the property value changes. /// public event EventHandler IsCheckedChanged { add => AddHandler(IsCheckedChangedEvent, value); remove => RemoveHandler(IsCheckedChangedEvent, value); } /// /// Defines the event. /// public static readonly RoutedEvent IsCheckedChangedEvent = RoutedEvent.Register( nameof(IsCheckedChanged), RoutingStrategies.Bubble); /// /// Defines the property. /// public static readonly StyledProperty IsCheckedProperty = AvaloniaProperty.Register( nameof(IsChecked)); //////////////////////////////////////////////////////////////////////// // Constructor / Destructors //////////////////////////////////////////////////////////////////////// /// /// Initializes a new instance of the class. /// public ToggleSplitButton() { } //////////////////////////////////////////////////////////////////////// // Properties //////////////////////////////////////////////////////////////////////// /// /// Gets or sets a value indicating whether the is checked. /// public bool IsChecked { get => GetValue(IsCheckedProperty); set => SetValue(IsCheckedProperty, value); } /// internal override bool InternalIsChecked => IsChecked; /// /// /// Both and share /// the same exact default style. /// Type IStyleable.StyleKey => typeof(SplitButton); //////////////////////////////////////////////////////////////////////// // Methods //////////////////////////////////////////////////////////////////////// /// /// Toggles the property between true and false. /// protected void Toggle() { IsChecked = !IsChecked; } //////////////////////////////////////////////////////////////////////// // OnEvent Overridable Methods //////////////////////////////////////////////////////////////////////// /// protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == IsCheckedProperty) { OnIsCheckedChanged(); } } /// /// Invokes the event when the /// property changes. /// protected virtual void OnIsCheckedChanged() { // IsLoaded check if (Parent is not null) { var eventArgs = new RoutedEventArgs(IsCheckedChangedEvent); RaiseEvent(eventArgs); } UpdatePseudoClasses(); } /// protected override void OnClickPrimary(RoutedEventArgs? e) { Toggle(); base.OnClickPrimary(e); } } }