From 966c5a0c5e2599aaf87a70ce196686320daa77e3 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 25 May 2023 16:14:06 +0200 Subject: [PATCH] Added ControlAutomationPeer.FromElement. --- .../Automation/Peers/ControlAutomationPeer.cs | 20 +++++++++++++++++++ src/Avalonia.Controls/Control.cs | 6 ++++++ 2 files changed, 26 insertions(+) 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();