Skip to content

Commit b2e65e2

Browse files
committed
Added automation peer class for expander
1 parent 596fdfc commit b2e65e2

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using Windows.System;
77
using Windows.UI.Xaml;
8+
using Windows.UI.Xaml.Automation.Peers;
89
using Windows.UI.Xaml.Controls;
910
using Windows.UI.Xaml.Controls.Primitives;
1011
using Windows.UI.Xaml.Input;
@@ -76,6 +77,15 @@ protected virtual void OnCollapsed(EventArgs args)
7677
Collapsed?.Invoke(this, args);
7778
}
7879

80+
/// <summary>
81+
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
82+
/// </summary>
83+
/// <returns>An automation peer for this <see cref="Expander"/>.</returns>
84+
protected override AutomationPeer OnCreateAutomationPeer()
85+
{
86+
return new ExpanderAutomationPeer(this);
87+
}
88+
7989
private void ExpanderToggleButtonPart_KeyDown(object sender, KeyRoutedEventArgs e)
8090
{
8191
if (e.Key != VirtualKey.Enter)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Windows.UI.Xaml.Automation;
2+
using Windows.UI.Xaml.Automation.Peers;
3+
using Windows.UI.Xaml.Automation.Provider;
4+
5+
namespace Microsoft.Toolkit.Uwp.UI.Controls
6+
{
7+
/// <summary>
8+
/// Defines a framework element automation peer for the <see cref="Expander"/> control.
9+
/// </summary>
10+
public class ExpanderAutomationPeer : FrameworkElementAutomationPeer, IToggleProvider
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="ExpanderAutomationPeer"/> class.
14+
/// </summary>
15+
/// <param name="owner">
16+
/// The <see cref="Expander" /> that is associated with this <see cref="T:Windows.UI.Xaml.Automation.Peers.ExpanderAutomationPeer" />.
17+
/// </param>
18+
public ExpanderAutomationPeer(Expander owner)
19+
: base(owner)
20+
{
21+
}
22+
23+
/// <summary>Gets the toggle state of the control.</summary>
24+
/// <returns>The toggle state of the control, as a value of the enumeration.</returns>
25+
public ToggleState ToggleState => OwningExpander.IsExpanded ? ToggleState.On : ToggleState.Off;
26+
27+
private Expander OwningExpander
28+
{
29+
get
30+
{
31+
return Owner as Expander;
32+
}
33+
}
34+
35+
/// <summary>Cycles through the toggle states of a control.</summary>
36+
public void Toggle()
37+
{
38+
if (!IsEnabled())
39+
{
40+
throw new ElementNotEnabledException();
41+
}
42+
43+
OwningExpander.IsExpanded = !OwningExpander.IsExpanded;
44+
}
45+
46+
/// <summary>
47+
/// Gets the control type for the element that is associated with the UI Automation peer.
48+
/// </summary>
49+
/// <returns>The control type.</returns>
50+
protected override AutomationControlType GetAutomationControlTypeCore()
51+
{
52+
return AutomationControlType.Custom;
53+
}
54+
55+
/// <summary>
56+
/// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType,
57+
/// differentiates the control represented by this AutomationPeer.
58+
/// </summary>
59+
/// <returns>The string that contains the name.</returns>
60+
protected override string GetClassNameCore()
61+
{
62+
return Owner.GetType().Name;
63+
}
64+
65+
/// <summary>
66+
/// Called by GetName.
67+
/// </summary>
68+
/// <returns>
69+
/// Returns the first of these that is not null or empty:
70+
/// - Value returned by the base implementation
71+
/// - Name of the owning Expander
72+
/// - Expander class name
73+
/// </returns>
74+
protected override string GetNameCore()
75+
{
76+
string name = base.GetNameCore();
77+
if (!string.IsNullOrEmpty(name))
78+
{
79+
return name;
80+
}
81+
82+
if (this.OwningExpander != null)
83+
{
84+
name = this.OwningExpander.Name;
85+
}
86+
87+
if (string.IsNullOrEmpty(name))
88+
{
89+
name = this.GetClassName();
90+
}
91+
92+
return name;
93+
}
94+
95+
/// <summary>
96+
/// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface.
97+
/// </summary>
98+
/// <param name="patternInterface">A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration.</param>
99+
/// <returns>The object that supports the specified pattern, or null if unsupported.</returns>
100+
protected override object GetPatternCore(PatternInterface patternInterface)
101+
{
102+
switch (patternInterface)
103+
{
104+
case PatternInterface.Toggle:
105+
return this;
106+
}
107+
108+
return base.GetPatternCore(patternInterface);
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)