ExpandCollapsePattern.Expand Methode

Definition

Zeigt alle untergeordneten Knoten, Steuerelemente oder Inhalte für das AutomationElement an.Displays all child nodes, controls, or content of the AutomationElement.

public:
 void Expand();
public void Expand ();
member this.Expand : unit -> unit
Public Sub Expand ()

Ausnahmen

Beispiele

Im folgenden Beispiel wird ein- AutomationElement Element, das ein Menü Element darstellt, an eine Funktion weitergegeben, die versucht, das Menü Element auf der Grundlage seines aktuellen zu erweitern oder zu reduzieren ExpandCollapseState .In the following example, an AutomationElement representing a menu item is passed to a function that attempts to expand or collapse the menu item based on its current ExpandCollapseState.

///--------------------------------------------------------------------
/// <summary>
/// Obtains an ExpandCollapsePattern control pattern from an 
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A ExpandCollapsePattern object.
/// </returns>
///--------------------------------------------------------------------
private ExpandCollapsePattern GetExpandCollapsePattern(
    AutomationElement targetControl)
{
    ExpandCollapsePattern expandCollapsePattern = null;

    try
    {
        expandCollapsePattern =
            targetControl.GetCurrentPattern(
            ExpandCollapsePattern.Pattern)
            as ExpandCollapsePattern;
    }
    // Object doesn't support the ExpandCollapsePattern control pattern.
    catch (InvalidOperationException)
    {
        return null;
    }

    return expandCollapsePattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains an ExpandCollapsePattern control pattern from an 
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A ExpandCollapsePattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetExpandCollapsePattern( _
ByVal targetControl As AutomationElement) As ExpandCollapsePattern
    Dim expandCollapsePattern As ExpandCollapsePattern = Nothing

    Try
        expandCollapsePattern = DirectCast( _
        targetControl.GetCurrentPattern(expandCollapsePattern.Pattern), _
        ExpandCollapsePattern)
    Catch exc As InvalidOperationException
        ' Object doesn't support the ExpandCollapsePattern control pattern.
        Return Nothing
    End Try

    Return expandCollapsePattern

End Function 'GetExpandCollapsePattern    
///--------------------------------------------------------------------
/// <summary>
/// Programmatically expand or collapse a menu item.
/// </summary>
/// <param name="menuItem">
/// The target menu item.
/// </param>
///--------------------------------------------------------------------
private void ExpandCollapseMenuItem(
    AutomationElement menuItem)
{
    if (menuItem == null)
    {
        throw new ArgumentNullException(
            "AutomationElement argument cannot be null.");
    }

    ExpandCollapsePattern expandCollapsePattern =
        GetExpandCollapsePattern(menuItem);

    if (expandCollapsePattern == null)
    {
        return;
    }

    if (expandCollapsePattern.Current.ExpandCollapseState ==
        ExpandCollapseState.LeafNode)
    {
        return;
    }

    try
    {
        if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Expanded)
        {
            // Collapse the menu item.
            expandCollapsePattern.Collapse();
        }
        else if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed ||
            expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.PartiallyExpanded)
        {
            // Expand the menu item.
            expandCollapsePattern.Expand();
        }
    }
    // Control is not enabled
    catch (ElementNotEnabledException)
    {
        // TO DO: error handling.
    }
    // Control is unable to perform operation.
    catch (InvalidOperationException)
    {
        // TO DO: error handling.
    }
}
'''--------------------------------------------------------------------
''' <summary>
''' Programmatically expand or collapse a menu item.
''' </summary>
''' <param name="menuItem">
''' The target menu item.
''' </param>
'''--------------------------------------------------------------------
Private Sub ExpandCollapseMenuItem(ByVal menuItem As AutomationElement)
    If menuItem Is Nothing Then
        Throw New ArgumentNullException( _
        "AutomationElement argument cannot be null.")
    End If

    Dim expandCollapsePattern As ExpandCollapsePattern = _
    GetExpandCollapsePattern(menuItem)

    If expandCollapsePattern Is Nothing Then
        Return
    End If

    If expandCollapsePattern.Current.ExpandCollapseState = _
    ExpandCollapseState.LeafNode Then
        Return
    End If

    Try
        If expandCollapsePattern.Current.ExpandCollapseState = _
        ExpandCollapseState.Expanded Then
            ' Collapse the menu item.
            expandCollapsePattern.Collapse()

        ElseIf expandCollapsePattern.Current.ExpandCollapseState = _
        ExpandCollapseState.Collapsed OrElse _
        expandCollapsePattern.Current.ExpandCollapseState = _
        ExpandCollapseState.PartiallyExpanded Then
            ' Expand the menu item.
            expandCollapsePattern.Expand()
        End If
    Catch exc As ElementNotEnabledException
        ' Control is not enabled
        ' TO DO: error handling.
    Catch exc As InvalidOperationException
        ' Control is unable to perform operation 
        ' TO DO: error handling.
    End Try

End Sub

Hinweise

Dies ist eine blockierende Methode, die zurückgibt, nachdem der AutomationElement erweitert wurde.This is a blocking method that returns after the AutomationElement has been expanded.

Es gibt Fälle, in denen eine AutomationElement , die als Blattknoten markiert ist, möglicherweise nicht weiß, ob Sie untergeordnete Elemente aufweist, bis entweder die- Collapse oder die- Expand Methode aufgerufen wird.There are cases when a AutomationElement that is marked as a leaf node might not know whether it has children until either the Collapse or the Expand method is called. Dieses Verhalten ist bei einem Strukturansicht-Steuerelement möglich, das die untergeordneten Elemente verzögert lädt.This behavior is possible with a tree view control that does delayed loading of its child items. Beispielsweise kann Microsoft Windows-Explorer das Erweiterungs Symbol für einen Knoten anzeigen, obwohl derzeit keine untergeordneten Elemente vorhanden sind. Wenn auf das Symbol geklickt wird, ruft das Steuerelement nach untergeordneten Elementen ab, findet keine und entfernt das Erweiterungs Symbol.For example, Microsoft Windows Explorer might display the expand icon for a node even though there are currently no child items; when the icon is clicked, the control polls for child items, finds none, and removes the expand icon. In diesen Fällen sollten Clients ein Eigenschaften Änderungs Ereignis für die Eigenschaft überwachen, ExpandCollapseState indem Sie einen Ereignishandler bei der- AddAutomationPropertyChangedEventHandler Methode registrieren.In these cases clients should listen for a property-changed event on the ExpandCollapseState property by registering an event handler with the AddAutomationPropertyChangedEventHandler method.

Gilt für: