Export (0) Print
Expand All

SelectionItemPattern.AddToSelection Method

Adds the current element to the collection of selected items.

Namespace:  System.Windows.Automation
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

public void AddToSelection()

ExceptionCondition
InvalidOperationException

An attempt is made to add a selection to a single-selection container where CanSelectMultipleProperty = false and another element is already selected.

The following example shows how to add an element to the collection of currently selected items.

///-------------------------------------------------------------------- 
/// <summary> 
/// Retrieves the selection container for a selection item. 
/// </summary> 
/// <param name="selectionItem">
/// An automation element that supports SelectionItemPattern. 
/// </param> 
///-------------------------------------------------------------------- 
private AutomationElement GetSelectionItemContainer(
    AutomationElement selectionItem)
{
    // Selection item cannot be null 
    if (selectionItem == null)
    {
        throw new ArgumentException();
    }

    SelectionItemPattern selectionItemPattern = 
        selectionItem.GetCurrentPattern(SelectionItemPattern.Pattern) 
        as SelectionItemPattern;
    if (selectionItemPattern == null)
    {
        return null;
    }
    else
    {
        return selectionItemPattern.Current.SelectionContainer; 
    }
}


...


///-------------------------------------------------------------------- 
/// <summary> 
/// Attempts to add the current item to a collection  
/// of selected items.  
/// </summary> 
/// <param name="selectionItem">
/// An automation element that supports SelectionItemPattern. 
/// </param> 
///-------------------------------------------------------------------- 
private void AddItemToSelection(AutomationElement selectionItem)
{
    if (selectionItem == null)
    {
        throw new ArgumentNullException(
            "Argument cannot be null or empty.");
    }

    AutomationElement selectionContainer = 
        GetSelectionItemContainer(selectionItem);

    // Selection container cannot be null 
    if (selectionContainer == null)
    {
        throw new ElementNotAvailableException();
    }

    SelectionPattern selectionPattern = 
        selectionContainer.GetCurrentPattern(SelectionPattern.Pattern)
        as SelectionPattern;

    if (selectionPattern == null)
    {
        return;
    }

    if (selectionPattern.Current.CanSelectMultiple)
    {
        SelectionItemPattern selectionItemPattern =
            selectionItem.GetCurrentPattern(
            SelectionItemPattern.Pattern)
            as SelectionItemPattern;
        if (selectionItemPattern != null)
        {
            try
            {
                selectionItemPattern.AddToSelection();
            }
            catch (InvalidOperationException)
            {
                // Unable to add to selection 
                return;
            }
        }
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft