diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs index 4602691a123..10eb403c663 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs @@ -3,8 +3,10 @@ // See the LICENSE file in the project root for more information. using System; +using Microsoft.Toolkit.Uwp.UI.Automation.Peers; using Windows.System; using Windows.UI.Xaml; +using Windows.UI.Xaml.Automation.Peers; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Input; @@ -76,6 +78,15 @@ protected virtual void OnCollapsed(EventArgs args) Collapsed?.Invoke(this, args); } + /// + /// Creates AutomationPeer () + /// + /// An automation peer for this . + protected override AutomationPeer OnCreateAutomationPeer() + { + return new ExpanderAutomationPeer(this); + } + private void ExpanderToggleButtonPart_KeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key != VirtualKey.Enter) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/ExpanderAutomationPeer.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/ExpanderAutomationPeer.cs new file mode 100644 index 00000000000..5e18110d63e --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/ExpanderAutomationPeer.cs @@ -0,0 +1,116 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Toolkit.Uwp.UI.Controls; +using Windows.UI.Xaml.Automation; +using Windows.UI.Xaml.Automation.Peers; +using Windows.UI.Xaml.Automation.Provider; + +namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers +{ + /// + /// Defines a framework element automation peer for the control. + /// + public class ExpanderAutomationPeer : FrameworkElementAutomationPeer, IToggleProvider + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The that is associated with this . + /// + public ExpanderAutomationPeer(Expander owner) + : base(owner) + { + } + + /// Gets the toggle state of the control. + /// The toggle state of the control, as a value of the enumeration. + public ToggleState ToggleState => OwningExpander.IsExpanded ? ToggleState.On : ToggleState.Off; + + private Expander OwningExpander + { + get + { + return Owner as Expander; + } + } + + /// Cycles through the toggle states of a control. + public void Toggle() + { + if (!IsEnabled()) + { + throw new ElementNotEnabledException(); + } + + OwningExpander.IsExpanded = !OwningExpander.IsExpanded; + } + + /// + /// Gets the control type for the element that is associated with the UI Automation peer. + /// + /// The control type. + protected override AutomationControlType GetAutomationControlTypeCore() + { + return AutomationControlType.Custom; + } + + /// + /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType, + /// differentiates the control represented by this AutomationPeer. + /// + /// The string that contains the name. + protected override string GetClassNameCore() + { + return Owner.GetType().Name; + } + + /// + /// Called by GetName. + /// + /// + /// Returns the first of these that is not null or empty: + /// - Value returned by the base implementation + /// - Name of the owning Expander + /// - Expander class name + /// + protected override string GetNameCore() + { + string name = base.GetNameCore(); + if (!string.IsNullOrEmpty(name)) + { + return name; + } + + if (this.OwningExpander != null) + { + name = this.OwningExpander.Name; + } + + if (string.IsNullOrEmpty(name)) + { + name = this.GetClassName(); + } + + return name; + } + + /// + /// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface. + /// + /// A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration. + /// The object that supports the specified pattern, or null if unsupported. + protected override object GetPatternCore(PatternInterface patternInterface) + { + switch (patternInterface) + { + case PatternInterface.Toggle: + return this; + } + + return base.GetPatternCore(patternInterface); + } + } +}