From a68cce869f044d726b943388b40d26f3dfe20b2a Mon Sep 17 00:00:00 2001 From: robloo Date: Sun, 15 Jan 2023 19:53:09 -0500 Subject: [PATCH] Add CancelRoutedEventArgs and use it in Expander --- .../ControlCatalog/Pages/ExpanderPage.xaml.cs | 4 +- .../Interactivity/CancelRoutedEventArgs.cs | 39 +++++++++++++++++++ src/Avalonia.Controls/Expander.cs | 26 ++++++------- 3 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/Avalonia.Base/Interactivity/CancelRoutedEventArgs.cs 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 93f5e0fc14..c57774c70b 100644 --- a/src/Avalonia.Controls/Expander.cs +++ b/src/Avalonia.Controls/Expander.cs @@ -84,16 +84,16 @@ namespace Avalonia.Controls /// /// Defines the event. /// - public static readonly RoutedEvent ExpandedEvent = - RoutedEvent.Register( + public static readonly RoutedEvent ExpandedEvent = + RoutedEvent.Register( nameof(Expanded), RoutingStrategies.Bubble); /// /// Defines the event. /// - public static readonly RoutedEvent ExpandingEvent = - RoutedEvent.Register( + public static readonly RoutedEvent ExpandingEvent = + RoutedEvent.Register( nameof(Expanding), RoutingStrategies.Bubble); @@ -149,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); @@ -171,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); @@ -295,22 +295,22 @@ namespace Avalonia.Controls /// The value to coerce. protected virtual bool OnCoerceIsExpanded(bool value) { - RoutedEventArgs eventArgs; + CancelRoutedEventArgs eventArgs; if (value) { - eventArgs = new RoutedEventArgs(ExpandingEvent, this); + eventArgs = new CancelRoutedEventArgs(ExpandingEvent, this); OnExpanding(eventArgs); } else { - eventArgs = new RoutedEventArgs(CollapsingEvent, this); + eventArgs = new CancelRoutedEventArgs(CollapsingEvent, this); OnCollapsing(eventArgs); } - if (eventArgs.Handled) + if (eventArgs.Cancel) { - // If the event was externally handled (canceled) we must still notify the value has changed. + // 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.