diff --git a/src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs b/src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs
index c55bd0f3e5..756951667b 100644
--- a/src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs
+++ b/src/Avalonia.Controls/Automation/Peers/ControlAutomationPeer.cs
@@ -31,11 +31,31 @@ namespace Avalonia.Automation.Peers
return CreatePeerForElement(element);
}
+ ///
+ /// Gets the for a , creating it if
+ /// necessary.
+ ///
+ /// The control.
+ /// The automation peer.
+ ///
+ /// Despite the name (which comes from the analogous WPF API), this method does not create
+ /// a new peer if one already exists: instead it returns the existing peer.
+ ///
public static AutomationPeer CreatePeerForElement(Control element)
{
return element.GetOrCreateAutomationPeer();
}
+ ///
+ /// Gets an existing for a .
+ ///
+ /// The control.
+ /// The automation peer if already created; otherwise null.
+ ///
+ /// To ensure that a peer is created, use .
+ ///
+ public static AutomationPeer? FromElement(Control element) => element.GetAutomationPeer();
+
protected override void BringIntoViewCore() => Owner.BringIntoView();
protected override IReadOnlyList GetOrCreateChildrenCore()
diff --git a/src/Avalonia.Controls/Control.cs b/src/Avalonia.Controls/Control.cs
index 13e3978ca6..8d140bdd62 100644
--- a/src/Avalonia.Controls/Control.cs
+++ b/src/Avalonia.Controls/Control.cs
@@ -440,6 +440,12 @@ namespace Avalonia.Controls
return new NoneAutomationPeer(this);
}
+ internal AutomationPeer? GetAutomationPeer()
+ {
+ VerifyAccess();
+ return _automationPeer;
+ }
+
internal AutomationPeer GetOrCreateAutomationPeer()
{
VerifyAccess();