مشاركة عبر


FrameworkElementAutomationPeer.FromElement(UIElement) Method

Definition

Returns the FrameworkElementAutomationPeer for the specified UIElement.

public:
 static AutomationPeer ^ FromElement(UIElement ^ element);
 static AutomationPeer FromElement(UIElement const& element);
public static AutomationPeer FromElement(UIElement element);
function fromElement(element)
Public Shared Function FromElement (element As UIElement) As AutomationPeer

Parameters

element
UIElement

The UIElement that is associated with this FrameworkElementAutomationPeer.

Returns

The FrameworkElementAutomationPeer, or null if the FrameworkElementAutomationPeer could not be created.

Examples

The automation support design doesn't retain a handle to your own peer as part of how you implement OnCreateAutomationPeer, because there aren't any guarantees of when the peer is actually created. Instead, you can check for run-time automation event listeners inside your control class definitions just-in-time, using code like this:

if (AutomationPeer.ListenerExists(AutomationEvents.PropertyChanged))
{
    MyAutomationPeer peer = 
        FrameworkElementAutomationPeer.FromElement(myCtrl) as MyAutomationPeer;

    if (peer != null)
    {
        peer.RaisePropertyChangedEvent(
            RangeValuePatternIdentifiers.ValueProperty,
            (double)oldValue,
            (double)newValue);
    }
}
If AutomationPeer.ListenerExists(AutomationEvents.PropertyChanged) Then
    Dim peer As MyDownAutomationPeer = _
    TryCast(FrameworkElementAutomationPeer.FromElement(myCtrl), MyAutomationPeer)
    If peer IsNot Nothing Then
        peer.RaisePropertyChangedEvent(RangeValuePatternIdentifiers.ValueProperty, CDbl(oldValue), CDbl(newValue))
    End If
End If

Remarks

FromElement is a helper class that is used by custom control code to return the acting peer instance. Using the returned peer, you can fire automation events from within the same routines that also fire your general control logic events or change control properties. Or you can write your own helper methods that do this and are invoked from your control logic.

CreatePeerForElement has basically the same behavior as FromElement.

If FromElement returns null, it's probably because the element you passed does not have an implementation for OnCreateAutomationPeer.

FromElement can return the peer even if the CreateAutomationPeer system logic hasn't run yet on the element target for some reason. It will invoke the same CreateAutomationPeer logic internally in order to get the peer.

Applies to

See also