diff --git a/samples/Sandbox/MainWindow.axaml b/samples/Sandbox/MainWindow.axaml index 6929f192c7..044d595f4b 100644 --- a/samples/Sandbox/MainWindow.axaml +++ b/samples/Sandbox/MainWindow.axaml @@ -1,4 +1,9 @@ + + + Expanded content + + diff --git a/src/Avalonia.Controls/Automation/Peers/ExpanderAutomationPeer.cs b/src/Avalonia.Controls/Automation/Peers/ExpanderAutomationPeer.cs new file mode 100644 index 0000000000..99566cdd4c --- /dev/null +++ b/src/Avalonia.Controls/Automation/Peers/ExpanderAutomationPeer.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using Avalonia.Automation; +using Avalonia.Automation.Peers; +using Avalonia.Automation.Provider; + +namespace Avalonia.Controls.Automation.Peers +{ + public class ExpanderAutomationPeer : ControlAutomationPeer, IExpandCollapseProvider + { + public ExpanderAutomationPeer(Expander owner) : base(owner) + { + owner.PropertyChanged += Owner_PropertyChanged; + } + + protected override string? GetNameCore() + { + return base.GetNameCore(); + } + protected override string GetClassNameCore() + { + return "Expander"; + } + protected override bool IsContentElementCore() => true; + protected override bool IsControlElementCore() => true; + protected override AutomationControlType GetAutomationControlTypeCore() + { + return AutomationControlType.Group; + } + + public ExpandCollapseState ExpandCollapseState => ((Expander)Owner).IsExpanded ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed; + + public bool ShowsMenu => throw new NotImplementedException(); + + public void Collapse() + { + if (!IsEnabled()) + throw new ElementNotEnabledException(); + + ((Expander)Owner).IsExpanded = false; + } + + public void Expand() + { + if (!IsEnabled()) + throw new ElementNotEnabledException(); + + ((Expander)Owner).IsExpanded = true; + } + + + private void Owner_PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e) + { + if (e.Property == Expander.IsExpandedProperty) + { + RaisePropertyChangedEvent( + ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty, + (bool)e.OldValue! ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed, + (bool)e.NewValue! ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed); + } + } + + } +} diff --git a/src/Avalonia.Controls/Expander.cs b/src/Avalonia.Controls/Expander.cs index 2ad6a58d38..607dfcc876 100644 --- a/src/Avalonia.Controls/Expander.cs +++ b/src/Avalonia.Controls/Expander.cs @@ -1,6 +1,8 @@ using System; using System.Threading; using Avalonia.Animation; +using Avalonia.Automation.Peers; +using Avalonia.Controls.Automation.Peers; using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Data; @@ -274,6 +276,11 @@ namespace Avalonia.Controls } } + protected override AutomationPeer OnCreateAutomationPeer() + { + return new ExpanderAutomationPeer(this); + } + /// /// Updates the visual state of the control by applying latest PseudoClasses. ///