diff --git a/samples/ControlCatalog/Pages/ExpanderPage.xaml.cs b/samples/ControlCatalog/Pages/ExpanderPage.xaml.cs index 98a494e533..c33a0d8bad 100644 --- a/samples/ControlCatalog/Pages/ExpanderPage.xaml.cs +++ b/samples/ControlCatalog/Pages/ExpanderPage.xaml.cs @@ -14,8 +14,8 @@ namespace ControlCatalog.Pages var CollapsingDisabledExpander = this.Get("CollapsingDisabledExpander"); var ExpandingDisabledExpander = this.Get("ExpandingDisabledExpander"); - CollapsingDisabledExpander.Collapsing += (s, e) => { e.Handled = true; }; - ExpandingDisabledExpander.Expanding += (s, e) => { e.Handled = true; }; + CollapsingDisabledExpander.Collapsing += (s, e) => { e.Cancel = true; }; + ExpandingDisabledExpander.Expanding += (s, e) => { e.Cancel = true; }; } private void InitializeComponent() diff --git a/src/Avalonia.Base/Interactivity/CancelRoutedEventArgs.cs b/src/Avalonia.Base/Interactivity/CancelRoutedEventArgs.cs new file mode 100644 index 0000000000..b6913939ab --- /dev/null +++ b/src/Avalonia.Base/Interactivity/CancelRoutedEventArgs.cs @@ -0,0 +1,39 @@ +namespace Avalonia.Interactivity +{ + /// + /// Provides state information and data specific to a cancelable routed event. + /// + public class CancelRoutedEventArgs : RoutedEventArgs + { + /// + /// Initializes a new instance of the class. + /// + public CancelRoutedEventArgs() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The routed event associated with these event args. + public CancelRoutedEventArgs(RoutedEvent? routedEvent) + : base(routedEvent) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The routed event associated with these event args. + /// The source object that raised the routed event. + public CancelRoutedEventArgs(RoutedEvent? routedEvent, object? source) + : base(routedEvent, source) + { + } + + /// + /// Gets or sets a value indicating whether the routed event should be canceled. + /// + public bool Cancel { get; set; } = false; + } +} diff --git a/src/Avalonia.Controls/Expander.cs b/src/Avalonia.Controls/Expander.cs index 65227a826a..2ad6a58d38 100644 --- a/src/Avalonia.Controls/Expander.cs +++ b/src/Avalonia.Controls/Expander.cs @@ -59,12 +59,11 @@ namespace Avalonia.Controls /// /// Defines the property. /// - public static readonly DirectProperty IsExpandedProperty = - AvaloniaProperty.RegisterDirect( + public static readonly StyledProperty IsExpandedProperty = + AvaloniaProperty.Register( nameof(IsExpanded), - o => o.IsExpanded, - (o, v) => o.IsExpanded = v, - defaultBindingMode: Data.BindingMode.TwoWay); + defaultBindingMode: BindingMode.TwoWay, + coerce: CoerceIsExpanded); /// /// Defines the event. @@ -77,8 +76,8 @@ namespace Avalonia.Controls /// /// Defines the event. /// - public static readonly RoutedEvent CollapsingEvent = - RoutedEvent.Register( + public static readonly RoutedEvent CollapsingEvent = + RoutedEvent.Register( nameof(Collapsing), RoutingStrategies.Bubble); @@ -93,13 +92,12 @@ namespace Avalonia.Controls /// /// Defines the event. /// - public static readonly RoutedEvent ExpandingEvent = - RoutedEvent.Register( + public static readonly RoutedEvent ExpandingEvent = + RoutedEvent.Register( nameof(Expanding), RoutingStrategies.Bubble); private bool _ignorePropertyChanged = false; - private bool _isExpanded; private CancellationTokenSource? _lastTransitionCts; /// @@ -134,50 +132,8 @@ namespace Avalonia.Controls /// public bool IsExpanded { - get => _isExpanded; - set - { - // It is important here that IsExpanded is a direct property so events can be invoked - // BEFORE the property system gets notified of updated values. This is because events - // may be canceled by external code. - if (_isExpanded != value) - { - RoutedEventArgs eventArgs; - - if (value) - { - eventArgs = new RoutedEventArgs(ExpandingEvent, this); - OnExpanding(eventArgs); - } - else - { - eventArgs = new RoutedEventArgs(CollapsingEvent, this); - OnCollapsing(eventArgs); - } - - if (eventArgs.Handled) - { - // If the event was externally handled (canceled) we must still notify the value has changed. - // This property changed notification will update any external code observing this property that itself may have set the new value. - // We are essentially reverted any external state change along with ignoring the IsExpanded property set. - // Remember IsExpanded is usually controlled by a ToggleButton in the control theme. - _ignorePropertyChanged = true; - - RaisePropertyChanged( - IsExpandedProperty, - oldValue: value, - newValue: _isExpanded, - BindingPriority.LocalValue, - isEffectiveValue: true); - - _ignorePropertyChanged = false; - } - else - { - SetAndRaise(IsExpandedProperty, ref _isExpanded, value); - } - } - } + get => GetValue(IsExpandedProperty); + set => SetValue(IsExpandedProperty, value); } /// @@ -193,10 +149,10 @@ namespace Avalonia.Controls /// Occurs as the content area is closing. /// /// - /// The event args property may be set to true to cancel the event + /// The event args property may be set to true to cancel the event /// and keep the control open (expanded). /// - public event EventHandler? Collapsing + public event EventHandler? Collapsing { add => AddHandler(CollapsingEvent, value); remove => RemoveHandler(CollapsingEvent, value); @@ -215,10 +171,10 @@ namespace Avalonia.Controls /// Occurs as the content area is opening. /// /// - /// The event args property may be set to true to cancel the event + /// The event args property may be set to true to cancel the event /// and keep the control closed (collapsed). /// - public event EventHandler? Expanding + public event EventHandler? Expanding { add => AddHandler(ExpandingEvent, value); remove => RemoveHandler(ExpandingEvent, value); @@ -332,5 +288,63 @@ namespace Avalonia.Controls PseudoClasses.Set(":expanded", IsExpanded); } + + /// + /// Called when the property has to be coerced. + /// + /// The value to coerce. + protected virtual bool OnCoerceIsExpanded(bool value) + { + CancelRoutedEventArgs eventArgs; + + if (value) + { + eventArgs = new CancelRoutedEventArgs(ExpandingEvent, this); + OnExpanding(eventArgs); + } + else + { + eventArgs = new CancelRoutedEventArgs(CollapsingEvent, this); + OnCollapsing(eventArgs); + } + + if (eventArgs.Cancel) + { + // If the event was externally canceled we must still notify the value has changed. + // This property changed notification will update any external code observing this property that itself may have set the new value. + // We are essentially reverted any external state change along with ignoring the IsExpanded property set. + // Remember IsExpanded is usually controlled by a ToggleButton in the control theme and is also used for animations. + _ignorePropertyChanged = true; + + RaisePropertyChanged( + IsExpandedProperty, + oldValue: value, + newValue: !value, + BindingPriority.LocalValue, + isEffectiveValue: true); + + _ignorePropertyChanged = false; + + return !value; + } + + return value; + } + + /// + /// Coerces/validates the property value. + /// + /// The instance. + /// The value to coerce. + /// The coerced/validated value. + private static bool CoerceIsExpanded(AvaloniaObject instance, bool value) + { + if (instance is Expander expander) + { + return expander.OnCoerceIsExpanded(value); + } + + return value; + } } }